str_rst
str_rst

Reputation: 175

calculating count of non zero values in row in dataframe

I am trying to calculate count on non zero elements in row in dat frame but not working.

condition: just want to exclude one column while calculating count of non zero elements in row

i just want to select T_1_1,T_1_2,T_1_3.....and exclude T_1_0 and create a new column and check how many non zero elements available in row. what i am doing wrog here ..??

df <- data.frame("T_1_1"= c(68,24,0,105,58,0,135,126,24),
                 "T_1_2"=c(26,105,0,73,39,97,46,108,0),
                 "T_1_3"=c(93,32,73,103,149,0,147,113,139),
                 "S_2_1"=c(69,67,94,0,77,136,137,92,73),
                 "S_2_2"=c(87,67,47,120,85,122,0,96,79),
                 "S_2_3"= c(150,0,132,121,29,78,109,40,0),
                 "T_1_0"= c(79,0,0,115,98,12,15,121,2)
                 
                 
)


df <- df %>% select(matches("^T_1_1"),-"T_1_0") %>% mutate(new1 = rowSums(matches("^T_1_1")!=0))

Upvotes: 0

Views: 547

Answers (2)

Anoushiravan R
Anoushiravan R

Reputation: 21908

This also can be a solution for your problem:

library(purrr)

df %>%
  mutate(exclu_T_1_0 = pmap(df %>% select(starts_with("T_1") & !ends_with("0")), 
                            ~ sum(c(...) != 0)))

  T_1_1 T_1_2 T_1_3 S_2_1 S_2_2 S_2_3 T_1_0 exclu_T_1_0
1    68    26    93    69    87   150    79           3
2    24   105    32    67    67     0     0           3
3     0     0    73    94    47   132     0           1
4   105    73   103     0   120   121   115           3
5    58    39   149    77    85    29    98           3
6     0    97     0   136   122    78    12           1
7   135    46   147   137     0   109    15           3
8   126   108   113    92    96    40   121           3
9    24     0   139    73    79     0     2           2

Upvotes: 2

Ronak Shah
Ronak Shah

Reputation: 388982

You can use the regex 'T_1_[1-9]' to exclude T_1_0.

library(dplyr)
df <- df %>% mutate(new1 = rowSums(select(., matches('T_1_[1-9]')) !=0))
df
#  T_1_1 T_1_2 T_1_3 S_2_1 S_2_2 S_2_3 T_1_0 new1
#1    68    26    93    69    87   150    79    3
#2    24   105    32    67    67     0     0    3
#3     0     0    73    94    47   132     0    1
#4   105    73   103     0   120   121   115    3
#5    58    39   149    77    85    29    98    3
#6     0    97     0   136   122    78    12    1
#7   135    46   147   137     0   109    15    3
#8   126   108   113    92    96    40   121    3
#9    24     0   139    73    79     0     2    2

Or more specific :

df <- df %>% mutate(new1 = rowSums(select(., starts_with('T_1'), -T_1_0) !=0))

Upvotes: 1

Related Questions