在乐谱中指定“NA在下标指定中不被允许”(Assigning within a lapply “NAs are not allowed in subscripted assignments”)

编程入门 行业动态 更新时间:2024-10-21 19:49:50
在乐谱中指定“NA在下标指定中不被允许”(Assigning within a lapply “NAs are not allowed in subscripted assignments”)

我正在尝试重新编码,并且遇到了一个看起来很简单的障碍,但是在相当一段时间问互联网之后我还没弄清楚,所以我感谢你能给予的任何帮助。

我有一些包含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 factors

Any 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 2

Your 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

更多推荐

本文发布于:2023-04-28 02:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330292.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:下标   乐谱   不被   NA   Assigning

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!