Home > other >  How to 'count' number of non-empty values in a single row across multiple columns in a dat
How to 'count' number of non-empty values in a single row across multiple columns in a dat

Time:02-01

I have a data frame that is similar as follows:

Comp1  Comp2  Comp3  Comp4
0.5      0.4    na     0.6
0.6      na     na      0.7
na       0.4    na      1.1

Each row represents a different person. For each person I want to calculate how many non-empty values they have in that specific row across those columns and save this as a new variable

thanks !

CodePudding user response:

If you are talking about missing values in R, it's represented in capital letter NA instead of na, otherwise, R will treat it as a string, which is not empty.

Also, I have artificially included some Name in your df to act like each row represents one Name, and a artificial Comp5 which includes some NAs but will not be included in the calculation.

rowSums() as its name suggests, calculates the sum of the row.
is.na(df[, 2:4]) makes it only counts the NA in df from column 2 to column 4.

df <-read.table(header = T, 
                text = 
                  "Name Comp1  Comp2  Comp3  Comp4  Comp5
                A 0.5      0.4    NA     0.6  NA
                B 0.6      NA     NA      0.7 1
                C NA       0.4    NA      1.1 NA")

df$Count_NA <- rowSums(is.na(df[, 2:4]))

Output

  Name Comp1 Comp2 Comp3 Comp4 Comp5 Count_NA
1    A   0.5   0.4    NA   0.6    NA        1
2    B   0.6    NA    NA   0.7     1        2
3    C    NA   0.4    NA   1.1    NA        2
  •  Tags:  
  • Related