按各自的 ID 重命名列表中的数据框名称

Rename data frame names in a list by its respective ID

我有一个包含 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:

您可以使用 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"

如果您只是像亲爱的@Ronak 在 中提到的那样在另一个问题中添加一个带有 setNames 的步骤怎么办?我是不是哪里漏了?

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

我们也可以

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