R * not meaningful for factors ERROR

非 Y 不嫁゛ 提交于 2019-12-17 09:51:57

问题


I have the following data.frame and I want to perform some calculations on the 2nd column.

> test  
  code age
1  101  15
2  102  25
3  103  16
4  104  u1
5  105  u1
6  106  u2
7  107  27
8  108  27

As you can see, the 2nd column does not include only numbers. I omitted these cases:

> new<-subset(test,code<104 | code>106)
> new
  code age
1  101  15
2  102  25
3  103  16
7  107  27
8  108  27

But when I try to do a calculation in a new column this is what I get:

> new["MY_NEW_COLUMN"] <- NA
> new
  code age MY_NEW_COLUMN
1  101  15            NA
2  102  25            NA
3  103  16            NA
7  107  27            NA
8  108  27            NA
> new$MY_NEW_COLUMN <-new[,2] * 5
Warning message:
In Ops.factor(new[, 2], 5) : * not meaningful for factors   

Why does that happen? Any suggestions?


回答1:


new[,2] is a factor, not a numeric vector. Transform it first

new$MY_NEW_COLUMN <-as.numeric(as.character(new[,2])) * 5



回答2:


Uou need a package named robustfa and its dependencies. This worked for me.



来源:https://stackoverflow.com/questions/30525292/r-not-meaningful-for-factors-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!