priya
priya

Reputation: 375

Get row and column names of particular values in dataframe

I am very new to R. I have a dataframe and i want to extract the rows and column names which have high and low values. My real data is very large. I have given below an example dataframe.

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

I want output like:

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

I tried like this. But the output is not easy to intrepret.

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 )

Upvotes: 1

Views: 645

Answers (1)

Nicolas2
Nicolas2

Reputation: 2210

You can do that with 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

Upvotes: 3

Related Questions