Home > Net >  Select Columns Based on Other Column's Value Using R
Select Columns Based on Other Column's Value Using R

Time:12-20

I have the following dataset, and I need to subset the dataset with columns based on first column value:

df <- data.frame(players=c('A', 'B', 'C', 'D', 'E'),
                 rebounds=c("", 3, "", "", ""),
                 assists=c("", 9, 4, 4, 3),
                 ratings=c("", 8, 4, 7, ""))

df

players rebounds    assists ratings
<chr>   <chr>        <chr>  <chr>
A           
B         3           9       8
C                     4       4
D                     4       7
E                     3                                                                   ​               ​        ​

I need to subset the columns which has "non-empty value" for the player D. In our example, only the last two columns have value for the player D. Hence, only the last 2 columns, along with the 1st column, would be selected.

Desired Output

players assists ratings
<chr>   <chr>   <chr>
A       
B        9        8
C        4        4
D        4        7

What would be the ideal way of getting the desired output? Any suggestions would be appreciated. Thanks!

CodePudding user response:

Not a beautiful solution, but this would work

library(dplyr)


df %>% 
  select(
    df %>% 
      filter(players == "D") %>% 
      select(where(function(x) x != "")) %>% 
      names()
  )

CodePudding user response:

With where nzchar:

library(dplyr)
df %>% 
  select(where(~ nzchar(.x[df$players == "D"])))
  players assists ratings
1       A                
2       B       9       8
3       C       4       4
4       D       4       7
5       E       3        

CodePudding user response:

You can isolate the row of interest (in this case 4 for player 'D') and get the indices of empty cells and subset, i.e.

df[-which(df[4,] == '')]

  players assists ratings
1       A                
2       B       9       8
3       C       4       4
4       D       4       7
5       E       3        

You can also make the 4 dynamic, i.e.

which(df$players == 'D')
#[1] 4
  • Related