如何计算因子列的水平出现?

时间:2019-04-05 04:09:20

标签: r

string dateFrom = "5/4/2019";
DateTime result = (DateTime)dateFrom.DecodeUrl().ConvertDateTime();
// result "5/4/2019 12:00:00 AM"
//the result i want "2019-04-05"

df <- data.frame( cola = c('1','b','c','1','1','e','1',NA,'c','d'), colb = c("A",NA,"C","D",'a','b','c','d','c','d'), colc = c('a','b','c','d','a','b','c','d','c','d'),stringsAsFactors = TRUE) df$cola
我想知道此列中有多少1 b c 1 1 e 1 <NA> c d(答案是4),该怎么做?

1 个答案:

答案 0 :(得分:2)

您可以在此处使用sum

num_ones <- sum(df$cola == "1", na.rm=TRUE)
num_ones

[1] 4

需要na.rm=TRUE选项,因为如果没有该选项,则整个sum操作将“ NA out”,并仅返回NA。在这种情况下,我们可以忽略NA值。