Get column names where dat is equal to

后端 未结 3 1326
Happy的楠姐
Happy的楠姐 2021-01-12 18:12

I have a data frame (df) with 7 rows and 4 columns (named c1, c2, c3, c4):

c1  c2  c3  c4
Yes No  Yes No    
Yes Yes No  No    
No  Yes No  No    
Yes No  No         


        
3条回答
  •  失恋的感觉
    2021-01-12 19:02

    An option using data.table

    library(data.table)
    setDT(df)[, rownum:=1:.N,]
    df$Expected_result <- melt(df, "rownum")[, 
                             toString(variable[value=="Yes"]), rownum]$V1
    

提交回复
热议问题