Adam G
Adam G

Reputation: 125

How to identify which columns are not “NA” per row in a dataframe?

This is the same question as How to identify which columns are not "NA" per row in a matrix? but for a dataframe and not a matrix.

I have a dataframe with 205 rows and 62 columns. I would like to identify the column names for each row that do not have NA as their value. E.g.:

        col1 col2 col3 col4 col5 col6
row1    NA   NA   NA   NA   fact fact
row2    num  num  NA   NA   NA   NA
row3    num  num  NA   NA   int  int    
row4    NA   NA   NA   NA   fact fact
row5    NA   NA   int  int  NA   NA
row6    NA   NA   int  NA   NA   NA

The result should then be:

row1    col5;col6
row2    col1;col2
row3    col1;col2;col5;col6
row4    col5;col6
row5    col3;col4
row6    col3

Ideally the result should be added to the dataframe as an extra column.

Upvotes: 0

Views: 936

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388797

Similar logic to @zx8754 using dplyr you could do :

library(dplyr)

df %>%
  tibble::rownames_to_column('row') %>%
  rowwise() %>%
  mutate(res = toString(names(df)[!is.na(c_across(starts_with('col')))])) %>%
  select(row, res)

#   row   res                   
#  <chr> <chr>                 
#1 row1  col5, col6            
#2 row2  col1, col2            
#3 row3  col1, col2, col5, col6
#4 row4  col5, col6            
#5 row5  col3, col4            
#6 row6  col3               

Upvotes: 1

zx8754
zx8754

Reputation: 56004

Loop through rows, get column names with for non-na columns, then paste:

d$myCol <- apply(d, 1, function(i) paste(colnames(d)[ !is.na(i) ], collapse = ","))

Upvotes: 2

Related Questions