R将因子与值进行比较

时间:2015-02-14 04:00:42

标签: 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.character(V2) > 100000, "L","none")))
    return(income_level)

}

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

请帮忙。

1 个答案:

答案 0 :(得分:0)

您可以修改功能

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"

或者也可以尝试使用cut

 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"