【问题标题】:How to modify select rows in the same column from all nested lists within a list?如何从列表中的所有嵌套列表中修改同一列中的选择行?
【发布时间】:2019-09-17 13:32:48
【问题描述】:

我有一个嵌套的项目列表,因此我将 3 个单独的列表组合为一个。我想对所有列表中存在的特定列进行更改。我在下面有更多详细信息

X

  $`Sports`
    Name    Start.Date
    Hockey  2019-07-01
    Soccer  2019-07-01
    Tennis  2019-07-01
    Track   2019-07-01

  $`Course`
    Name    Start.Date
    Math    2019-07-01
    English 2019-07-01
    Biology 2019-07-01
    Spanish 2019-07-01
    Physics 2019-07-01

  $`Elective`
    Name    Start.Date
    Ballet  2019-07-01
    Arts    2019-07-01
    Chess   2019-07-01
    Piano   2019-07-01
    Dance   2019-07-01
    Flute   2019-07-01
    Reading 2019-07-01

我只想修改“Start.Date”列

lapply(X, '[', 'Start.Date') 给我:

  $`Sports`
    Start.Date
    2019-07-01
    2019-07-01
    2019-07-01
    2019-07-01

  $`Course`
    Start.Date
    2019-07-01
    2019-07-01
    2019-07-01
    2019-07-01
    2019-07-01

  $`Elective`
    Start.Date
    2019-07-01
    2019-07-01
    2019-07-01
    2019-07-01
    2019-07-01
    2019-07-01
    2019-07-01

但是希望只保留每个列表项中第一行的日期,使其如下所示:

  $`Sports`
    Name    Start.Date
    Hockey  2019-07-01
    Soccer  NA
    Tennis  NA
    Track   NA

  $`Course`
    Name    Start.Date
    Math    2019-07-01
    English NA
    Biology NA
    Spanish NA
    Physics NA

  $`Elective`
    Name    Start.Date
    Ballet  2019-07-01
    Arts    NA
    Chess   NA
    Piano   NA
    Dance   NA
    Flute   NA
    Reading NA

我如何实现这一目标?

【问题讨论】:

    标签: r


    【解决方案1】:

    这里有两种可能的方法:

    library(tidyverse)
    
    X <- list(
      Sports = data.frame(
        Name = c("Hockey", "Soccer", "Tennis", "Track"),
        Start.Date = rep(as.Date("2019-07-01"), 4)
      ),
      Course = data.frame(
        Name = c("Math", "English", "Biology", "Spanish", "Physics"),
        Start.Date = rep(as.Date("2019-07-01"), 5)
      ),
      Elective = data.frame(
        Name = c("Ballet", "Arts", "Chess", "Piano", "Dance", "Flute", "Reading"),
        Start.Date = rep(as.Date("2019-07-01"), 7)
      )
    )
    
    X
    #> $Sports
    #>     Name Start.Date
    #> 1 Hockey 2019-07-01
    #> 2 Soccer 2019-07-01
    #> 3 Tennis 2019-07-01
    #> 4  Track 2019-07-01
    #> 
    #> $Course
    #>      Name Start.Date
    #> 1    Math 2019-07-01
    #> 2 English 2019-07-01
    #> 3 Biology 2019-07-01
    #> 4 Spanish 2019-07-01
    #> 5 Physics 2019-07-01
    #> 
    #> $Elective
    #>      Name Start.Date
    #> 1  Ballet 2019-07-01
    #> 2    Arts 2019-07-01
    #> 3   Chess 2019-07-01
    #> 4   Piano 2019-07-01
    #> 5   Dance 2019-07-01
    #> 6   Flute 2019-07-01
    #> 7 Reading 2019-07-01
    
    X %>%
      map(
        . %>% 
          mutate(
            Start.Date = c(Start.Date[1], rep(NA, length(Start.Date) - 1))
          )
      )
    #> $Sports
    #>     Name Start.Date
    #> 1 Hockey 2019-07-01
    #> 2 Soccer       <NA>
    #> 3 Tennis       <NA>
    #> 4  Track       <NA>
    #> 
    #> $Course
    #>      Name Start.Date
    #> 1    Math 2019-07-01
    #> 2 English       <NA>
    #> 3 Biology       <NA>
    #> 4 Spanish       <NA>
    #> 5 Physics       <NA>
    #> 
    #> $Elective
    #>      Name Start.Date
    #> 1  Ballet 2019-07-01
    #> 2    Arts       <NA>
    #> 3   Chess       <NA>
    #> 4   Piano       <NA>
    #> 5   Dance       <NA>
    #> 6   Flute       <NA>
    #> 7 Reading       <NA>
    
    lapply(X, function(df) { 
      transform(df, Start.Date = c(Start.Date[1], rep(NA, length(Start.Date) - 1))) 
    })
    #> $Sports
    #>     Name Start.Date
    #> 1 Hockey 2019-07-01
    #> 2 Soccer       <NA>
    #> 3 Tennis       <NA>
    #> 4  Track       <NA>
    #> 
    #> $Course
    #>      Name Start.Date
    #> 1    Math 2019-07-01
    #> 2 English       <NA>
    #> 3 Biology       <NA>
    #> 4 Spanish       <NA>
    #> 5 Physics       <NA>
    #> 
    #> $Elective
    #>      Name Start.Date
    #> 1  Ballet 2019-07-01
    #> 2    Arts       <NA>
    #> 3   Chess       <NA>
    #> 4   Piano       <NA>
    #> 5   Dance       <NA>
    #> 6   Flute       <NA>
    #> 7 Reading       <NA>
    

    【讨论】:

    • 感谢这帮了大忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    相关资源
    最近更新 更多