dbfk2000
dbfk2000

Reputation: 25

How to compare cells to specific cell in the same row?

I have a table with four columns with the raw data. On the fifth column, I calculated mean per row. For example, 17.50=(1+22+28+19)/4 Now, I would like to compare the mean value to each cell in the same row, so it is that 1 vs 17.50, 22 vs 17.50, 28 vs 17.50, 19 vs 17.50).

Then, if the cell is greater than the mean value, it is counted as 1, otherwise 0.

With the example, it is 0, 1, 1, 1. Finally, I want to sum them up and put the number in the last column. Thus, the first value of the comparison column becomes 3.

Please anybody help me to make a code in R. Thanks in advance a lot!!

enter image description here

a<-c(1, 94, 19, 62, 20, 74, 20, 35, 52, 78)
b<-c(22, 7, 70, 25, 57, 100, 25, 45, 26, 34)
c<-c(28, 97,69, 76, 4, 72, 25, 8, 35, 72)
d<-c(19, 18, 7, 26, 59, 88, 33, 100, 44, 59)

data<-data.frame(a, b, c, d)
data$mean <- apply(data, 1, mean)
data$comparison<-"NA"

Upvotes: 0

Views: 65

Answers (1)

Onyambu
Onyambu

Reputation: 79318

Here is how you would do this:. use rowMeans for the means and rowSums to find the count:

cbind(data, comparison = rowSums(data >rowMeans(data)))
    a   b  c   d comparison
1   1  22 28  19          3
2  94   7 97  18          2
3  19  70 69   7          2
4  62  25 76  26          2
5  20  57  4  59          2
6  74 100 72  88          2
7  20  25 25  33          1
8  35  45  8 100          1
9  52  26 35  44          2
10 78  34 72  59          2

If you need to include all the calculations, try within function in base R:

within(data,{means = rowMeans(data);comparison = rowSums(data>means)})
    a   b  c   d comparison means
1   1  22 28  19          3 17.50
2  94   7 97  18          2 54.00
3  19  70 69   7          2 41.25
4  62  25 76  26          2 47.25
5  20  57  4  59          2 35.00
6  74 100 72  88          2 83.50
7  20  25 25  33          1 25.75
8  35  45  8 100          1 47.00
9  52  26 35  44          2 39.25
10 78  34 72  59          2 60.75

This will be cleaner in tidyverse:

library(tidyverse)
data %>%
   mutate(means = rowMeans(cur_data()),
          comparison = rowSums(cur_data() > means))

    a   b  c   d means comparison
1   1  22 28  19 17.50          3
2  94   7 97  18 54.00          2
3  19  70 69   7 41.25          2
4  62  25 76  26 47.25          2
5  20  57  4  59 35.00          2
6  74 100 72  88 83.50          2
7  20  25 25  33 25.75          1
8  35  45  8 100 47.00          1
9  52  26 35  44 39.25          2
10 78  34 72  59 60.75          2

Upvotes: 1

Related Questions