-2

I have a dataframe df:

a b c
1 5 5
2 3 5
3 3 5
3 3 3
3 3 2
4 2 2
1 2 2

I want to calculate how much 3's I have in a row for example, how can I do it? For example row 2 = 1, row 3 = 2 etc. Please advice.

Dr. Richard Tennen
  • 267
  • 1
  • 2
  • 9

3 Answers3

2

You can use apply and table for this. The output is a list giving you the counts of unique elements per row. (If this is of interest, setting the MARGIN of apply to 2 would give you the output per column.)

Update: Since others have provided solutions producing more "ordered" output in the meanwhile, I have amended my approach by using data.table::rbindlist for this purpose.

#I have skipped some of the last rows of your example
data <- read.table(text = "
                   a b c
                   1 5 5
                   2 3 5
                   3 3 5
                   3 3 3
                   ", header = T, stringsAsFactors = F)

apply(data, 1, table)
# [[1]]
# 1 5 
# 1 2 
# [[2]]
# 2 3 5 
# 1 1 1 
# [[3]]
# 3 5 
# 2 1 
# [[4]]
# 3 
# 3 

#Update: output in more ordered fashion 
library(data.table)
rbindlist(apply(data, 1, function(x) as.data.table(t(as.matrix(table(x)))))
          ,fill = TRUE
          ,use.names = TRUE)
#     1  5  2  3
# 1:  1  2 NA NA
# 2: NA  1  1  1
# 3: NA  1 NA  2
# 4: NA NA NA  3

#if necessary NA values might be replaced, see, e.g.,
##https://stackoverflow.com/questions/7235657/fastest-way-to-replace-nas-in-a-large-data-table
Manuel Bickel
  • 2,156
  • 2
  • 11
  • 22
2

The answer of @ManuelBickel is good if you want to count all of the values. If you really just want to know how many 3's there are, this might be simpler.

rowSums(data==3)
[1] 0 1 2 3
G5W
  • 36,531
  • 10
  • 47
  • 80
2

If you want the counts returned in a more ordered fashion

set.seed(1)
m <- matrix(sample(c(1:3, 5), 15, replace=TRUE), 5, dimnames=list(LETTERS[1:5]))
m
#   [,1] [,2] [,3]
# A    2    5    1
# B    2    5    1
# C    3    3    3
# D    5    3    2
# E    1    1    5

u <- sort(unique(as.vector(m)))
r <- sapply(setNames(u, u), function(x) rowSums(m == x))
r
#   1 2 3 5
# A 1 1 0 1
# B 1 1 0 1
# C 0 0 3 0
# D 0 1 1 1
# E 2 0 0 1
AkselA
  • 8,153
  • 2
  • 21
  • 34