I have looked through other answers for this eror code and they do bnot resolve my problem.
I am trying to change the level of one variable based on the level of another variable.
I am able to do this for two levels of the variable, but not the third, which is when I get the error message:
Warning message:
In [<-.factor
(*tmp*
, supportdf$Q7a == "I did not need any additional support", :
invalid factor level, NA generated
It is hard to replicate the error as I can't share the data set, but this is the closest approximation
var1<- c(NA, NA, NA, NA, NA, NA, "Yes, from my family", "Yes, from my family", NA, "Yes, from my family" )
var2<- c(NA, NA, NA, NA, NA, "Yes, from my friends", NA, NA, "Yes, from my friends", "Yes, from my friends" )
var3<- c("I did not need any additional support", NA, NA, NA, NA, NA, NA, "I did not need any additional support", NA, NA )
var4<- c(NA, "I did not need any additional support", "Yes, from my family", "Yes, from my friends", "I did not need any additional support", NA, NA, NA, NA, NA )
df<- data.frame(var1, var2, var3, var4)
df
var1 var2 var3 var4
1 <NA> <NA> I did not need any additional support <NA>
2 <NA> <NA> <NA> I did not need any additional support
3 <NA> <NA> <NA> Yes, from my family
4 <NA> <NA> <NA> Yes, from my friends
5 <NA> <NA> <NA> I did not need any additional support
6 <NA> Yes, from my friends <NA> <NA>
7 Yes, from my family <NA> <NA> <NA>
8 Yes, from my family <NA> I did not need any additional support <NA>
9 <NA> Yes, from my friends <NA> <NA>
10 Yes, from my family Yes, from my friends <NA> <NA>
Ultimately I want to change var1, var2 and var3 based on the var4 value and then change var4 to NA as below
var1a<- c(NA, NA, "Yes, from my family", NA, NA, NA, "Yes, from my family", "Yes, from my family", NA, "Yes, from my family" )
var2a<- c(NA, NA, NA, "Yes, from my friends", NA, "Yes, from my friends", NA, NA, "Yes, from my friends", "Yes, from my friends" )
var3a<- c("I did not need any additional support", "I did not need any additional support", NA, NA, "I did not need any additional support", NA, NA, "I did not need any additional support", NA, NA )
var4a<- c(NA, NA,NA, NA , NA, NA, NA, NA, NA, NA )
dfa<- data.frame(var1a, var2a, var3a, var4a)
dfa
var1a var2a var3a var4a
1 <NA> <NA> I did not need any additional support NA
2 <NA> <NA> I did not need any additional support NA
3 Yes, from my family <NA> <NA> NA
4 <NA> Yes, from my friends <NA> NA
5 <NA> <NA> I did not need any additional support NA
6 <NA> Yes, from my friends <NA> NA
7 Yes, from my family <NA> <NA> NA
8 Yes, from my family <NA> I did not need any additional support NA
9 <NA> Yes, from my friends <NA> NA
10 Yes, from my family Yes, from my friends <NA> NA
I've been doing it like this
df$var1[df$var4 == "Yes, from my family" ]<- "Yes, from my family"
df$var4[df$var4 == "Yes, from my family" ]<- NA
df$var2[df$var4== "Yes, from my friends" ]<- "Yes, from my friends"
df$var4[df$var4 == "Yes, from my friends" ]<- NA
but when I get to var3 (in my dataset, not in a made up example) I get the error message
df$var3[df$var4 == "I did not need any additional support" ]<- "I did not need any additional support"
Warning message:
In `[<-.factor`(`*tmp*`, supportdf$Q7a == "I did not need any additional support", :
#invalid factor level, NA generated
Why would this be the case only for this level? I've also tried with an if clause but that doesn't work and I get this error message
df<- if (df$var4 == "I did not need any additional support")df$Qvar3<-"I did not need any additional support"
Error in if (supportdf$Q7a == "I did not need any additional support") supportdf$Q7.6 <- "I did not need any additional support" :
missing value where TRUE/FALSE needed
In addition: Warning message:
In if (supportdf$Q7a == "I did not need any additional support") supportdf$Q7.6 <- "I did not need any additional support" :
the condition has length > 1 and only the first element will be used
Any help or advice appreciated