获取数据框中特定值的行和列名称

时间:2018-09-28 07:53:45

标签: r tidyverse

我对R非常陌生。我有一个数据框,我想提取具有高值和低值的行和列名称。我的真实数据非常大。我在下面给出了一个示例数据框。

   A         B       C       D  
1  middle   middle   middle  high
2  high     middle   middle  high
3  high     low      middle  middle
4  middle   middle   middle  middle

我想要输出:

row  column  
 1    D=high
 2    A,D= high
 3    A=high,B=low

我尝试过这样。但是输出并不容易理解。

which(apply(df, 1, function(r) any(r %in% c("high", "low"))))
 (returns row which has value)
colnames(df)[which(df == "high", arr.ind = TRUE)[,"col"]] 
 (returns column names )

1 个答案:

答案 0 :(得分:3)

您可以使用tidyverse做到这一点:

df %>% mutate(row=row_number()) %>%
  gather(k,v,-row) %>% filter(v %in% c("low","high")) %>% 
  group_by(row,v) %>% summarize(k=paste(k,collapse=",")) %>%
  summarize(r=paste(paste(k,v,sep='='),collapse=", "))
## A tibble: 3 x 2
#    row r            
#  <int> <chr>        
#1     1 D=high       
#2     2 A,D=high     
#3     3 A=high, B=low