Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R count函数提供了行数_R_Count - Fatal编程技术网

R count函数提供了行数

R count函数提供了行数,r,count,R,Count,我正在试图找到整个数据帧中每行值的出现次数 count(df,"name of column") 现在我得到的只是行的总数,而不是每个值的出现次数 谢谢 在data.table中,我们可以使用内置的.N计数器函数来获取给定列中每个唯一值出现的次数: library(data.table) df <- setDT(df) # no quotes in the column name counts <- df[,.(.N), by = name_of_column

我正在试图找到整个数据帧中每行值的出现次数

count(df,"name of column")

现在我得到的只是行的总数,而不是每个值的出现次数


谢谢

在data.table中,我们可以使用内置的.N计数器函数来获取给定列中每个唯一值出现的次数:

library(data.table)
df <- setDT(df)
# no quotes in the column name
counts <- df[,.(.N), by = name_of_column]
库(data.table)

df问题在于列名周围的引号。使用
iris
数据集进行演示:

library(dplyr)
带引号(错误):

无引号(正确):


您是否正在使用dplyr
?base R中没有
count()
函数。如果包含一个简单的示例输入和所需的输出,可以用来测试和验证可能的解决方案,则可以更容易地为您提供帮助。数据的图像没有帮助,因为我们无法复制/粘贴要测试的值。你把名字用引号引起来了吗?如果是这样,请尝试不使用引号,只使用列名。如果列名有不寻常的字符,请使用
`
作为引号,而不是
删除列名周围的引号”“谢谢!我意识到这是因为我需要plyrOK:所以是的,
plyr
版本的
count
使用引用的变量。但是我建议使用
dplyr
,它是
plyr
的一个改进的、更新的替代品。切勿同时加载
plyr
dplyr
,否则会导致问题。
count(iris, "Species")

  "Species"   n
1   Species 150
count(iris, Species)

     Species  n
1     setosa 50
2 versicolor 50
3  virginica 50