简体   繁体   English

如何在R中的特定列上按行计数?

[英]How to count by row across specific columns in R?

I have a dataframe like the following, where each row is a person and each column is an answer coded 0,1: 我有一个类似以下的数据框,其中每一行是一个人,每一列是一个编码为0,1的答案:

data<-as.data.frame(cbind('answer1' = c(0,0,1,0,0,0), 
'answer2' = c(1,1,1,1,1,0),
'answer3' = c(1,1,1,0,1,1), 
'answer4' = c(1,0,0,0,0,0)))

What I would like to do is count, for each person, the number of "1"s in only certain columns and to reference those columns by column name, not number. 我想做的是为每个人计算仅某些列中的“ 1”数,并按列名而不是数字引用这些列。 In this case, "count the number of times 1 appears in "answer1" and "answer3" only." 在这种情况下,“仅计数1在“ answer1”和“ answer3”中出现的次数”。 So I want to end up with something that looks like: 因此,我想得出的最终结果是:

data<-as.data.frame(cbind('answer1' = c(0,0,1,0,0,0), 
'answer2' = c(1,1,1,1,1,0), 
'answer3' = c(1,1,1,0,1,1), 
'answer4' = c(1,0,0,0,0,0), 
'sum' = c(1,1,2,0,1,1)))

I've searched and have found a number of related questions but none addressing the specific issue of counting only certain columns and referencing those columns by name. 我已经搜索并找到了许多相关问题,但是都没有解决仅计算某些列并按名称引用这些列的特定问题。 I've tried rowSums and can use it to sum across all columns, but can't seem to get it to select only certain ones. 我已经尝试过rowSums,并且可以使用它对所有列进行求和,但是似乎无法让它仅选择某些列。 I'm sure there's a very easy answer to this but it's eluding me... Thank you! 我敢肯定对此有一个很简单的答案,但是这使我难以理解...谢谢!

edit: I need to actually count the number of instances of "1" rather than simply summing across the two columns, because some rows in the actual dataframe will contain values other than 1 or 0, which will interfere with using a simple summation. 编辑:我需要实际计算“ 1”的实例数量,而不是简单地在两列之间求和,因为实际数据帧中的某些行将包含非1或0的值,这将干扰使用简单的求和。 So, the example dataframe should have looked like this instead: 因此,示例数据框应该看起来像这样:

data<-as.data.frame(cbind('answer1' = c(0,0,1,0,2,0), 
'answer2' = c(1,1,1,1,1,0), 
'answer3' = c(1,1,1,0,1,1), 
'answer4' = c(1,0,0,0,0,0)))

Update 更新

Based on your edit, try: 根据您的编辑,尝试:

> rowSums(data[c("answer1", "answer3")] == 1)
[1] 1 1 2 0 1 1

Original answer 原始答案

Yes, rowSums is what you want: 是的, rowSums是您想要的:

> data$sum <- rowSums(data[c("answer1", "answer3")])
> data
  answer1 answer2 answer3 answer4 sum
1       0       1       1       1   1
2       0       1       1       0   1
3       1       1       1       0   2
4       0       1       0       0   0
5       0       1       1       0   1
6       0       0       1       0   1

There are, however, many other approaches. 但是,还有许多其他方法。 within (or transform ) is sometimes nice for these types of problems: within (或transform )有时对于以下类型的问题很有用:

within(data, {
  sum <- answer1 + answer3
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM