【问题标题】:How to select variables with numeric suffixes lower than a value如何选择数字后缀小于某个值的变量
【发布时间】:2021-11-07 05:39:12
【问题描述】:

我有一个类似于这个的数据框。

df <- data.frame(id=c(1,2,3), tot_1=runif(3, 0, 100), tot_2=runif(3, 0, 100), tot_3=runif(3, 0, 100), tot_4=runif(3, 0, 100))

我只想对后缀小于 3 的人进行选择或操作。

#select
df <- df %>% select(id, tot_1, tot_2)
#or sum
df <- df %>% mutate(sumVar = rowSums(across(c(tot_1, tot_2))))

但是,在我的真实数据中,变量更多,并且不是按顺序排列的。那我怎么能不手动选择它们呢?

【问题讨论】:

    标签: r select dplyr suffix


    【解决方案1】:

    解决方案num_range

    这是来自 dplyr 的经常被遗忘的 num_range 选择助手的罕见情况,它在一个步骤中从名称中提取数字,然后选择一个范围:

    确定阈值

    suffix_threshold <- 3
    

    选择()

    library(dplyr)
    
    df %>% select(id, num_range(prefix='tot_',
                                range=seq_len(suffix_threshold-1)))
    
      id    tot_1    tot_2
    1  1 26.75082 26.89506
    2  2 21.86453 18.11683
    3  3 51.67968 51.85761
    

    mutate() 与 rowSums()

    library(dplyr)
    
    df %>% mutate(sumVar = across(num_range(prefix='tot_', range=seq_len(suffix_threshold-1)))%>%
                          rowSums)
    
      id    tot_1    tot_2    tot_3    tot_4    sumVar
    1  1 26.75082 26.89506 56.27829 71.79353  53.64588
    2  2 21.86453 18.11683 12.91569 96.14099  39.98136
    3  3 51.67968 51.85761 25.63676 10.01408 103.53730
    

    【讨论】:

    • @user2246905 ,它适用于您的数据集吗?
    【解决方案2】:

    这是一个基本的 R 方式 -

    cols <- grep('tot_', names(df), value = TRUE)
    
    #Select
    df[c('id', cols[as.numeric(sub('tot_', '',cols)) < 3])]
    
    #  id     tot_1    tot_2
    #1  1 75.409112 30.59338
    #2  2  9.613496 44.96151
    #3  3 58.589574 64.90672
    
    #Rowsums
    df$sumVar <- rowSums(df[cols[as.numeric(sub('tot_', '',cols)) < 3]])
    df
    
    #  id     tot_1    tot_2    tot_3     tot_4    sumVar
    #1  1 75.409112 30.59338 59.82815 50.495758 106.00250
    #2  2  9.613496 44.96151 84.19916  2.189482  54.57501
    #3  3 58.589574 64.90672 18.17310 71.390459 123.49629
    

    【讨论】:

      【解决方案3】:

      我们可能会使用matches

      df %>% 
          mutate(sumVar = rowSums(across(matches('tot_[1-2]$'))))
      

      如果我们需要更灵活,请从以“tot”开头的列名中提取数字部分,根据条件进行子集并使用新名称

      library(stringr)
      nm1 <- str_subset(names(df), 'tot')
      nm2 <-  nm1[readr::parse_number(nm1) <3]
      df %>%
          mutate(sumVar = rowSums(across(all_of(nm2))))
      

      【讨论】:

      • 我的真实数据的范围大于 10,所以我需要 match('tot_[1-112]$') 之类的东西,但它不适用于这个范围
      猜你喜欢
      • 1970-01-01
      • 2020-10-10
      • 2019-09-25
      • 1970-01-01
      • 2019-01-15
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多