R将因子与值进行比较

R将因子与值进行比较,r,R,我想将因子与值进行比较。我有一个数据框 V1 V2 a 1,230,000 b 1,500,234 c 22,003,512 d 1,103,222 e 207,512 f 23,451 classify_V2 = function(V2) { income_level= ifelse(as.character(V2) > 10000000, "B", ifelse(as.character(V2) > 1000000, "M", ifelse(as.

我想将因子与值进行比较。我有一个数据框

V1  V2
a   1,230,000
b   1,500,234
c   22,003,512
d   1,103,222
e   207,512
f   23,451

classify_V2 = function(V2)
{
income_level=   ifelse(as.character(V2) > 10000000, "B", ifelse(as.character(V2) > 1000000, "M", ifelse(as.character(V2) > 100000, "L","none")))
    return(income_level)

}
我编写了上面的代码来对V2进行分类,但它没有返回适当的结果


请帮助。

您可以修改该功能

classify_V2 = function(V2){
 V2 <-  as.numeric(gsub(',', '', V2))

 income_level=   ifelse(V2 > 10000000, "B", ifelse(V2 > 1000000, "M", 
       ifelse(V2 > 100000, "L","none")))
return(income_level)
}
classify_V2(df1$V2)
#[1] "M"    "M"    "B"    "M"    "L"    "none"

看起来第二列是
字符
。结尾是否有逗号
1500234,
?将V2转换为数字,然后应用ifelse<代码>df$V2谢谢运行!!!我也有同样的想法,并尝试了as.numeric或as.integer,但它返回了荒谬的结果。@a在转换之前,您必须删除
 with(df1, as.character(cut(as.numeric(gsub(',', '', V2)), 
    breaks=c(-Inf,1e5,e6, 1e7, Inf), labels=c('none', 'L', 'M', 'B'))))
#[1] "M"    "M"    "B"    "M"    "L"    "none"