我正在尝试重新编码,并且遇到了一个看起来很简单的障碍,但是在相当一段时间问互联网之后我还没弄清楚,所以我感谢你能给予的任何帮助。
我有一些包含NA的数据。 我想使用这些数据进行重新编码,但继续遇到错误“在下载的分配中不允许使用NA”。 当我正在尝试创建一个示例数据集时,我还会遇到警告,我没有“有意义的因素”。 任何帮助,将不胜感激。
我的虚假数据有三个变量:“var1”和“var2”(字符,有时缺失)和“var3”(数字)。 我想创建第四个变量,如果beta大于零,则包含“var1”的值,如果beta小于零,则包含值“var2”。 如果缺少var1或var2,我希望新变量也丢失:
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]我得到的是一堆红色:
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任何意见,将不胜感激。 谢谢。
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 factorsAny advice would be appreciated. Thank you.
最满意答案
您的问题是您在cbind之前使用cbind ,这会将您的三个变量强制转换为同一个类(必须是字符),导致它们在您创建data.frame时被强制分解。
相反,只是做
df <- data.frame(var1, var2, var3)为newVar运行相同的代码,你应该得到:
var1 var2 var3 newVar 1 A G -0.1 2 2 T A 3.0 4 3 <NA> <NA> -4.0 NA 4 G A 5.0 3 5 C G -3.0 2Your problem is that you are using cbind before data.frame, this coerces your three variables into the same class (which has to be character), causing them to be coerced to factor when you make your data.frame.
Instead, just do
df <- data.frame(var1, var2, var3)Run the same code for newVar and you should get:
var1 var2 var3 newVar 1 A G -0.1 2 2 T A 3.0 4 3 <NA> <NA> -4.0 NA 4 G A 5.0 3 5 C G -3.0 2更多推荐
发布评论