简体   繁体   中英

Mutate with Number of occurrences in a data frame - R

I have a data frame as below. I want to count occurrences of 1 in each row and insert a new column with that information:

df1

    Cluster a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 
1      C1    0  1  1  1  0  1  1  1  1  1   1   
2      C2    0  1  1  1  0  1  1  1  1  1   1   
3      C3    0  1  0  0  0  0  1  0  0  0   0   
4      C4    0  1  1  1  0  1  1  1  1  1   1   
5      C5    0  1  1  1  0  1  1  1  1  1   1

I want the results to be as follows:

df1

    Cluster a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10  X 
1      C1    0  1  1  1  0  1  1  1  1  1   1  9   
2      C2    0  1  1  1  0  1  1  1  1  1   1  9 
3      C3    0  1  0  0  0  0  1  0  0  0   0  2 
4      C4    0  1  1  1  0  1  1  1  1  1   1  9 
5      C5    0  1  1  1  0  1  1  1  1  1   1  9

I prefer use of mutate function in dplyr

I like using select_if inside rowSums to generalize the chain.

Edit: If you need to address NA values, use the na.rm option in rowSums

df1 %>% 
   mutate(X = rowSums(select_if(., is.numeric) == 1, na.rm = TRUE))

May not be general. But this should work for your given case:

df1 %>% mutate(X = rowSums(.[-1] == 1))

#  Cluster a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 X
#1      C1  0  1  1  1  0  1  1  1  1  1   1 9
#2      C2  0  1  1  1  0  1  1  1  1  1   1 9
#3      C3  0  1  0  0  0  0  1  0  0  0   0 2
#4      C4  0  1  1  1  0  1  1  1  1  1   1 9
#5      C5  0  1  1  1  0  1  1  1  1  1   1 9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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