简体   繁体   English

使用 R 根据其他列的值选择列

[英]Select Columns Based on Other Column's Value Using R

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 .我需要对玩家D具有“非空值”的列进行子集化。 In our example, only the last two columns have value for the player D .在我们的示例中,只有最后两列对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!谢谢!

Not a beautiful solution, but this would work这不是一个漂亮的解决方案,但这会奏效

library(dplyr)


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

With where + nzchar :使用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        

You can isolate the row of interest (in this case 4 for player 'D') and get the indices of empty cells and subset, ie您可以隔离感兴趣的行(在本例中为玩家“D”的 4)并获取空单元格和子集的索引,即

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, ie您还可以使 4 动态化,即

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM