Assigning within a lapply "NAs are not allowed in subscripted assignments"
I am trying to recode, and am running into a snag that seems simple
enough, but I haven't been able to figure out after quite some time asking
the internet, so I appreciate any help you can give.
I have some data that contains NA's. I would like to recode, using this
data, but keep on running into the error "NAs are not allowed in
subscripted assignments." As I'm trying to create an example data set, I'm
additionally running into a warning that I don't have "meaningful
factors." Any help would be appreciated.
My faux-data has three variables: "var1" and "var2" (character, and
sometimes missing) and "var3" (numeric). I want to create a fourth
variable, that contains the value of "var1" if beta is greater than zero,
and contains the value of "var2" if beta is less than zero. If var1 or
var2 is missing, I want the new variable to also be missing:
var1<-c("A","T",NA,"G","C")
var2<-c("G","A",NA,"A","G")
var3 <-c(-.1,3,-4,5,-3)
df=as.data.frame(cbind(var1,var2,var3))
df$newVar[df$var3>0]=df$var1[df$var3>0]
df$newVar[df$var3<0]=df$var2[df$var3<0]
What I get is a bunch of red:
df$newVar[df$var3>0]=df$var1[df$var3>0]
Error in df$newVar[df$var3 > 0] = df$var1[df$var3 > 0] :
NAs are not allowed in subscripted assignments
In addition: Warning messages:
1: In Ops.factor(df$var3, 0) : > not meaningful for factors
2: In Ops.factor(df$var3, 0) : > not meaningful for factors
df$newVar[df$var3<0]=df$var2[df$var3<0]
Error in df$newVar[df$var3 < 0] = df$var2[df$var3 < 0] :
NAs are not allowed in subscripted assignments
In addition: Warning messages:
1: In Ops.factor(df$var3, 0) : < not meaningful for factors
2: In Ops.factor(df$var3, 0) : < not meaningful for factors
Any advice would be appreciated. Thank you.
No comments:
Post a Comment