0

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))

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry about the necro on this post.. but love this answer.. please could you explain though what sum(c(...) != 0)) does? the c(...) is throwing me off.
No it's indeed very simple. c(...) captures all the elements of a given row. In fact it is used with pmap in a row-wise operation. So after I excluded the columns the post owner would like to have, I captured all the elements of a given row with c(...) and sum up those that are not zero. I asked some questions in the past that could help you as there are good answers. Check my questions. One is below: stackoverflow.com/questions/67049561/using-pmap-with-c-part-2
1

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))

2 Comments

actually i have many columns like T_1_1,T_1_2,T_1_3... but i want to select column which matches "T_1_" but only unselect T_1_0 from data frame
You don't want to consider S_2_1, S_2_2 columns right? I think my updated answer should help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.