【问题标题】:Rename data frame names in a list by its respective ID通过其各自的 ID 重命名列表中的数据框名称
【发布时间】:2021-05-18 13:25:27
【问题描述】:

我有一个带有ID 的数据框列表,我想从数据框中提取ID 并将其分配给ID 所在的数据框的名称。

library(lubridate)
library(tidyverse)

date <- rep_len(seq(dmy("01-01-2010"), dmy("31-12-2013"), by = "days"),1000)
ID <- rep(c("A","B","C", "D", "E"), 100)

df <- data.frame(date = date,
                 x = runif(length(date), min = 60000, max = 80000),
                 y = runif(length(date), min = 800000, max = 900000),
                 ID)

df1 <- df %>% group_split(ID)

这是一个使用图像的示例,其中[[1]] 我想将其重命名为A,而[[2]] 我想将其重命名为B

【问题讨论】:

    标签: r dplyr tidyverse tidyr


    【解决方案1】:

    您可以使用sapply 从每个列表中提取ID 的第一个值并将其添加到名称中。

    names(df1) <- sapply(df1, function(x) x$ID[1])
    

    如果您需要tidyverse,也可以使用map_chr

    library(dplyr)
    library(purrr)
    
    names(df1) <- map_chr(df1, ~first(.x$ID))
    

    如果你使用base::split,问题会自动解决

    df1 <- split(df, df$ID)
    names(df1)
    #[1] "A" "B" "C" "D" "E"
    

    【讨论】:

    • 如果我想为名称使用两个标准,这可能吗?例如,如果每个数据框的年份不同,我想同时使用年份和 ID 作为名称。
    • 是的。 names(df1) &lt;- sapply(df1, function(x) paste(x$ID[1], x$year[1], sep = '_'))df1 &lt;- split(df, list(df$ID, df$year))
    【解决方案2】:

    如果您只是在另一个问题中添加一个步骤,如亲爱的@Ronak 在answer 中提到的那样,会怎样?我是不是错过了什么地方?

    df %>% group_split(ID) %>%
      setNames(unique(df$ID))
    
    <list_of<
      tbl_df<
        date: date
        x   : double
        y   : double
        ID  : character
      >
    >[5]>
    $A
    # A tibble: 200 x 4
       date            x       y ID   
       <date>      <dbl>   <dbl> <chr>
     1 2010-01-01 68763. 817010. A    
     2 2010-01-06 75343. 854334. A    
     3 2010-01-11 65251. 821344. A    
     4 2010-01-16 61032. 880614. A    
     5 2010-01-21 74299. 804467. A    
     6 2010-01-26 60853. 802548. A    
     7 2010-01-31 64105. 826663. A    
     8 2010-02-05 73516. 840242. A    
     9 2010-02-10 68840. 882013. A    
    10 2010-02-15 68969. 877367. A    
    # ... with 190 more rows
    
    $B
    # A tibble: 200 x 4
       date            x       y ID   
       <date>      <dbl>   <dbl> <chr>
     1 2010-01-02 68629. 892168. B    
     2 2010-01-07 60095. 878701. B    
     3 2010-01-12 77022. 843730. B    
     4 2010-01-17 74596. 891906. B    
     5 2010-01-22 65954. 875685. B    
     6 2010-01-27 66975. 896184. B    
     7 2010-02-01 67633. 808057. B    
     8 2010-02-06 69934. 857379. B    
     9 2010-02-11 63846. 803666. B    
    10 2010-02-16 75543. 817345. B    
    # ... with 190 more rows
    
    $C
    # A tibble: 200 x 4
       date            x       y ID   
       <date>      <dbl>   <dbl> <chr>
     1 2010-01-03 60550. 834531. C    
     2 2010-01-08 72072. 832031. C    
     3 2010-01-13 66672. 837873. C    
     4 2010-01-18 70963. 805862. C    
     5 2010-01-23 65670. 859167. C    
     6 2010-01-28 70847. 898763. C    
     7 2010-02-02 69451. 833014. C    
     8 2010-02-07 78045. 859699. C    
     9 2010-02-12 68698. 862558. C    
    10 2010-02-17 63164. 849321. C    
    # ... with 190 more rows
    
    $D
    # A tibble: 200 x 4
       date            x       y ID   
       <date>      <dbl>   <dbl> <chr>
     1 2010-01-04 62931. 808511. D    
     2 2010-01-09 78112. 883804. D    
     3 2010-01-14 71566. 896558. D    
     4 2010-01-19 75024. 847764. D    
     5 2010-01-24 76598. 851014. D    
     6 2010-01-29 72189. 863436. D    
     7 2010-02-03 76710. 889405. D    
     8 2010-02-08 71025. 857002. D    
     9 2010-02-13 64503. 843348. D    
    10 2010-02-18 77336. 873719. D    
    # ... with 190 more rows
    
    $E
    # A tibble: 200 x 4
       date            x       y ID   
       <date>      <dbl>   <dbl> <chr>
     1 2010-01-05 68452. 837782. E    
     2 2010-01-10 74133. 899847. E    
     3 2010-01-15 68655. 864112. E    
     4 2010-01-20 61015. 869688. E    
     5 2010-01-25 61728. 845260. E    
     6 2010-01-30 65427. 842554. E    
     7 2010-02-04 62429. 830668. E    
     8 2010-02-09 62571. 821807. E    
     9 2010-02-14 79222. 835693. E    
    10 2010-02-19 64123. 851100. E    
    # ... with 190 more rows
    

    【讨论】:

      【解决方案3】:

      我们也可以

      library(purrr)
      names(df1) <- map_chr(df1, ~.x$ID[1])
      

      【讨论】:

        猜你喜欢
        • 2018-01-04
        • 2019-01-10
        • 2021-02-22
        • 1970-01-01
        • 2018-04-12
        • 2018-12-21
        • 1970-01-01
        • 2021-07-08
        • 1970-01-01
        相关资源
        最近更新 更多