1

I have a dataframe like so: df

ID  Black  red   blue  green
1     0      1     1     1
2     0      0     2     1
3     0      5     1     2
4     0      0     1     2
5     0      0     1     2

with this data, how can I count the columns that have any number regardless of the actual number? Is there any way to do this?

I tried rowSums(df) but result is not what I want.

My desired output is:

ID  Black  red   blue  green    count
1     0      1     1     1        3
2     0      0     2     1        2
3     0      5     1     2        3
4     0      0     1     2        2
5     0      0     1     2        2

thanks in advance!

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
R starter
  • 197
  • 12

2 Answers2

2

I think this should work for you

data = data.frame(Black = rep(0, 5),
                  Red = c(1,0,5,0,0),
                  Blue = c(1,2,1,1,1),
                  Green = c(1,1,2,2,2))


data$count = apply(data, 1, FUN = function(x) length(x[x>0]))
s_scolary
  • 1,361
  • 10
  • 21
1

Another solution:

library(dplyr)

data <-
  data.frame(Black = rep(0, 5),
             Red = c(1,0,5,0,0),
             Blue = c(1,2,1,1,1),
             Green = c(1,1,2,2,2)
             )
data$count <-
  data %>%
  apply(2, as.logical) %>%
  rowSums

this produces the same result as the answer by ccharles, but utilizes a trick that I think is broadly useful. By coercing all values to the logical class (i.e. TRUE or FALSE), you can collapse all zeros into FALSE, and any other number into TRUE. Numeric functions treat logical values as TRUE = 1 and FALSE = 0, so you can then use rowSums() to get a count for each row.

Graeme Frost
  • 2,438
  • 2
  • 13
  • 15