计算 R 中一组变量中值的出现次数(每行) [英] Count occurrences of value in a set of variables in R (per row)
问题描述
假设我有一个包含 10 个数值变量 V1-V10(列)和多行(案例)的数据框.
Let's say I have a data frame with 10 numeric variables V1-V10 (columns) and multiple rows (cases).
我希望 R 做的是:对于每种情况,给我一组变量中某个值出现的次数.
What I would like R to do is: For each case, give me the number of occurrences of a certain value in a set of variables.
例如V2、V3、V6的那一行中数值99出现的次数,显然它的最小值为0(三者都没有99),最大值为3(全部为三者的值为 99).
For example the number of occurrences of the numeric value 99 in that single row for V2, V3, V6, which obviously has a minimum of 0 (none of the three have the value 99) and a maximum of 3 (all of the three have the value 99).
我真的在寻找相当于 SPSS 函数COUNT
:COUNT 创建一个数值变量,对于每种情况,计算相同值的出现次数(或值列表)跨变量列表."
I am really looking for an equivalent to the SPSS function COUNT
: "COUNT creates a numeric variable that, for each case, counts the occurrences of the same value (or list of values) across a list of variables."
我想过 table()
和库 plyr 的 count()
,但我真的想不通.首选矢量化计算.非常感谢!
I thought about table()
and library plyr's count()
, but I cannot really figure it out. Vectorized computation preferred. Thanks a lot!
推荐答案
尝试
apply(df,MARGIN=1,table)
其中 df
是您的 data.frame
.这将返回一个与 data.frame 中行数相同长度的列表.列表的每一项对应data.frame的一行(顺序相同),是一张表格,内容是出现次数,名称是对应的值.
Where df
is your data.frame
. This will return a list of the same length of the amount of rows in your data.frame. Each item of the list corresponds to a row of the data.frame (in the same order), and it is a table where the content is the number of occurrences and the names are the corresponding values.
例如:
df=data.frame(V1=c(10,20,10,20),V2=c(20,30,20,30),V3=c(20,10,20,10))
#create a data.frame containing some data
df #show the data.frame
V1 V2 V3
1 10 20 20
2 20 30 10
3 10 20 20
4 20 30 10
apply(df,MARGIN=1,table) #apply the function table on each row (MARGIN=1)
[[1]]
10 20
1 2
[[2]]
10 20 30
1 1 1
[[3]]
10 20
1 2
[[4]]
10 20 30
1 1 1
#desired result
这篇关于计算 R 中一组变量中值的出现次数(每行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!