【问题标题】:dplyr::select question: different selection according to the order in which variables are pickeddplyr::select 问题:根据选择变量的顺序进行不同的选择
【发布时间】:2021-02-07 20:13:50
【问题描述】:

我想我有一个非常简单的问题。请在下面找到两个 dplyr::select 命令。我正在使用 nycflights13::flights 数据集。第一个命令给了我我所期望的。我本来希望第二个渲染同样的东西,但事实并非如此。看起来好像没有注意“:”部分。有什么提示吗? 谢谢!

select(flights,dep_delay:arr_delay,-arr_time)

select(flights,-arr_time, dep_delay:arr_delay)

【问题讨论】:

    标签: r dplyr tidyverse


    【解决方案1】:

    在第二种情况下,当我们在列范围之前指定-arr_time时,它会选择除-arr_time之外的所有列,然后dep_delay:arr_delay只是多余的,而在第一种情况下,它已经是子集dep_delay:arr_delay first 的列

    names(select(flights,dep_delay:arr_delay))
    #[1] "dep_delay"      "arr_time"       "sched_arr_time" "arr_delay"  
    

    从此,我们将删除arr_time

    第二个,它将在所有列中删除-arr_time

    names(select(flights,-arr_time))
    #[1] "year"           "month"          "day"            "dep_time"       "sched_dep_time" "dep_delay"      "sched_arr_time" "arr_delay"     
    #[9] "carrier"        "flight"         "tailnum"        "origin"         "dest"           "air_time"       "distance"       "hour"          
    #[17] "minute"         "time_hour"  
    
     
    

    然后它重新引入arr_time,因为它是范围的一部分

    names(select(flights,-arr_time, dep_delay:arr_delay))
    #[1] "year"           "month"          "day"            "dep_time"       "sched_dep_time" "dep_delay"      "sched_arr_time" "arr_delay"     
    #[9] "carrier"        "flight"         "tailnum"        "origin"         "dest"           "air_time"       "distance"       "hour"          
    #[17] "minute"         "time_hour"      "arr_time"   
    

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 2021-11-24
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-11
      相关资源
      最近更新 更多