Nas Are Not Allowed in Subscripted Assignments

NAs are not allowed in subscripted assignments

Your logic will need to also exclude NAs in the subset. See the following example. Note the subsets vectors are stored away before x is modified.

x <- c(1,3,5,7,NA,2,4,6)
subset1 <- x>=5 & !is.na(x)
subset2 <- x<5 & !is.na(x)

x[subset1] <- which(subset1)
x[subset2] <- 10*which(subset2)

Dates error NAs are not allowed in subscripted assignments

The expression:

df$enddate[df$endui.year2 == as.Date("2015-12-31") & !is.na(df$endui.year3)]

Produces NA values:

#> [1] NA           "2015-12-31" NA  

These go away if you wrap the logical condition in which

df$enddate[which(df$endui.year2 == as.Date("2015-12-31") & !is.na(df$endui.year3))]
#> [1] "2014-12-31"

But you also need to have the same condition for the dates that you wish to move too (you need to select the endui.year2 value in the row where df$endui.year2 == as.Date("2015-12-31")) to move it to the correct place. Perhaps the easiest way to do this is to set the conditions first

condition1 <- which(df$endui.year1 == as.Date("2014-12-31") & !is.na(df$endui.year2))
df$enddate[condition1] <- df$endui.year2[condition1]

condition2 <- which(df$endui.year2 ==a s.Date("2015-12-31") & !is.na(df$endui.year3))
df$enddate[condition2] <- df$endui.year3[condition2]

Which results in:

df
#> id beginui endui.year1 endui.year2 endui.year3 endui.year4 enddate
#> 1 R007 2013-01-01 2014-12-31 <NA> <NA> <NA> 2014-12-31
#> 2 R008 2013-02-01 2014-10-31 <NA> 2016-10-01 2017-01-01 2014-10-31
#> 3 R009 2013-03-01 2014-12-31 2015-12-31 2016-06-01 <NA> 2016-06-01
#> 4 R010 2013-06-10 2014-12-31 <NA> 2016-10-10 2017-11-01 2014-12-31

NAs are not allowed in subscripted assignments

You're trying to assign to these three rows:

> match(dat$code, age.and.sex$code)
[1] 1 2 NA

because dat$code and age.and.sex$code are not the same length, so the third comparison is NA.

I'm not sure what you actually mean to be matching, but you might just try subsetting to the first two observations, or na.omit, etc.

But a better way to join data from two tables is to use a join.

library(data.table)
dat <- data.table(dat)
setkey(dat,code)
age.and.sex <- data.table(age.and.sex)
setkey(age.and.sex,code)
dat[age.and.sex]
> dat[age.and.sex]
code age sex more i.age i.sex
1: A11 NA m 7 15 m
2: B22 NA f 4 10 f

Note how the columns of the inner table get appended to those of the outer table.

More... Per @joran's suggestion...you can use this technique to fill in missing observations:

joined <- dat[age.and.sex]
joined[is.na(age),age:=i.age] #only replace the value missing from left table
joined[,c("i.age","i.sex"):=NULL]
joined
> joined
code age sex more
1: A11 15 m 7
2: B22 10 f 4

Update to address your comment...just reverse the join. There are some cleverer ways to do this less manually, but this should be simple to follow:

joined <- age.and.sex[dat]
joined[is.na(age),age:=i.age]
joined[is.na(sex),sex:=i.sex]
joined[,c("i.age","i.sex"):=NULL]
> joined
code age sex more
1: A11 15 m 7
2: B22 10 f 4
3: C33 12 m 9

If this technique is to your liking you should definitely read ?data.table and the related vignette to learn more about joins.

Error NAs are not allowed in subscripted assignments while using Squash_axis in ggplot2, with dataset without NA-values

Using browser() if figured out that the problem arises in the inv part of the squish transformation. But I could only guess what the reason is, probably has to do with how the breaks are set (??). However, instead of applying the transformation inside scale_y_continuous I tried applying it via coord_trans ... et voila, worked:

library(ggplot2)

dat <- data.frame(group=rep(c('A', 'B', 'C', 'D'), each = 10),
value=c(rnorm(10), rnorm(10)+100)
)

squash_axis <- function(from, to, factor) {
# A transformation function that squashes the range of [from, to] by factor on a given axis

# Args:
# from: left end of the axis
# to: right end of the axis
# factor: the compression factor of the range [from, to]
#
# Returns:
# A transformation called "squash_axis", which is capsulated by trans_new() function

trans <- function(x) {
# get indices for the relevant regions
isq <- x > from & x < to
ito <- x >= to

# apply transformation
x[isq] <- from + (x[isq] - from)/factor
x[ito] <- from + (to - from)/factor + (x[ito] - to)

return(x)
}

inv <- function(x) {

# get indices for the relevant regions
isq <- x > from & x < from + (to - from)/factor
ito <- x >= from + (to - from)/factor

# apply transformation
x[isq] <- from + (x[isq] - from) * factor
x[ito] <- to + (x[ito] - (from + (to - from)/factor))

return(x)
}

# return the transformation
return(scales::trans_new("squash_axis", trans, inv, domain = c(from, to)))
}

ggplot(dat,aes(x=group, y = value))+
geom_point()+
coord_trans(y = squash_axis(5, 95, 10))

Sample Image

Created on 2020-04-03 by the reprex package (v0.3.0)

Predicting multinom model in R - NAs are not allowed in subscripted assignments

Placeholder “answer”: It appears that one or more of my comments answered the question, so I will come back and assemble an answer to fill out this space after searching for prior answers that deal with formulas and evaluation environments.

I think debugging would be a lot easier if you temporarily ran those dataframes through check.names. Furthermore you are violating the cardinal rule of no "$"'s in formulas. (That might be where you are going wrong. Use a data= argument.)

I spend a fair amount of time searching for a duplicate and will reference some of the "good stuff" that I found but have not really found an answer that seems to make the points I was planning to describe. There's an additional point to be made regarding constructing Surv objects for use in survival regression formulas that is similar in its reasoning. Still haven had time to work on the body of hte answer though.

NAs are not allowed in subscripted assignments: error in predicting probability

If you would like to predict the probability of each category of response, you should use:

predict.vec <- predict(multinom.mod, test.df, type = "probs")

otherwise, the prediction is on class by default, type = class.

Update, a complete usage (training and predicting) should look like this:

require(nnet)

response1 <- sample(runif(100))
response2 <- 1 - response1

train <- data.frame(var1 = runif(100), var2 = runif(100))
# train with matrix
responses <- cbind(response1, response2)
multinom.mod <- multinom(responses ~ var1 + var2, train, type = "probs")
# train with category
train$response <- ifelse(response1 > response2, "response1", "response2")
multinom.mod1 <- multinom(response ~ var1 + var2, train)

test.df <- data.frame(var1 = runif(5), var2 = runif(5))
# no matter which training method you use,
# you can predict class (default) or probability
predict.cvec <- predict(multinom.mod, test.df, type = "class")
predict.pvec <- predict(multinom.mod, test.df, type = "probs")

predict.cvec1 <- predict(multinom.mod1, test.df, type = "class")
predict.pvec1 <- predict(multinom.mod1, test.df, type = "probs")


Related Topics



Leave a reply



Submit