计算R中的一组变量中的值的出现次数(每行) [英] Count occurrences of value in a set of variables in R (per row)

查看:4426
本文介绍了计算R中的一组变量中的值的出现次数(每行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含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.

例如,在该单个数值中出现的数字99 V2,V3,V6的行,其显然具有最小值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."

我考虑过表格()和库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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆