2

To count how many times one row is equal to a value

  1. I have a df here:
    df <- data.frame('v1'=c(1,2,3,4,5),
                 'v2'=c(1,2,1,1,2),
                 'v3'=c(NA,2,1,4,'1'),
                 'v4'=c(1,2,3,NaN,5),                
                 'logical'=c(1,2,3,4,5))
  1. I would like to know how many times one row is equal to the value of the variable 'logical' with a new variable 'count' I wrte a for loop like this:
    attach(df)
    df$count <- 0
    for(i in colnames(v1:v4)){
        if(df$logical == i){
        df$count <- df$count+1} 
       }

but it doesn't work. there's still all 0 in the new variable 'count'. Please help to fix it.

the perfect result should looks like this:

    df <- data.frame('v1'=c(1,2,3,4,5),
                 'v2'=c(1,2,1,1,2),
                 'v3'=c(NA,2,1,4,'1'),
                 'v4'=c(1,2,3,NaN,5),                
                 'logical'=c(1,2,3,4,5),
                 'count'=c(3,4,2,2,2))

Many thanks from a beginner.

sylvia
  • 197
  • 1
  • 8
  • Not directly related to the question, but I strongly suggest you avoid the `attach()` function. There's no need for it in this case at all if you are using `df$` each time. – MrFlick Dec 03 '19 at 16:01
  • I guess the `if` part is not correct in the `for` loop. it can be `ifelse` `ifelse(df$logical == df[[i]], ..` – akrun Dec 03 '19 at 16:20

2 Answers2

3

We can use rowSums after creating a logical matrix

df$count <- rowSums(df[1:4] == df$logical,  na.rm = TRUE)
df$count
#[1] 3 4 2 2 2
akrun
  • 874,273
  • 37
  • 540
  • 662
  • thank you very much! but I would to attach this result on the df in a elegent way. I will appreciate if there is a further answer. – sylvia Dec 03 '19 at 16:01
  • @sylvia If you meant the `attach` function, please don't use that – akrun Dec 03 '19 at 16:02
  • @sylvia do you mean something like `df <- within(df, {count <- rowSums(df[1:4] == logical, na.rm = TRUE)})`? – ThomasIsCoding Dec 03 '19 at 16:18
2

Personally I guess so far the solution by @akrun is an elegant and also the best efficient way to add the column count.

Another way (I don't know if that is the one you are looking for the "elegance") you can used to "attach" the column the count column to the end of df might be using within, i.e.,

df <- within(df, count <- rowSums(df[1:4]==logical,na.rm = T))

such that you will get

> df
  v1 v2   v3  v4 logical count
1  1  1 <NA>   1       1     3
2  2  2    2   2       2     4
3  3  1    1   3       3     2
4  4  1    4 NaN       4     2
5  5  2    1   5       5     2
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81