John Huang
John Huang

Reputation: 845

Rename data frame names in a list by its respective ID

I have a list of data frames with ID and I would like to name the extract the ID from the data frames and assign it to the name of the data frame the ID is located in.

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)

Here is an example using the image, where in [[1]] I would like to rename it to A and in [[2]] I would like to rename it to B:

enter image description here

Upvotes: 0

Views: 354

Answers (3)

akrun
akrun

Reputation: 887501

We can also do

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

Upvotes: 0

AnilGoyal
AnilGoyal

Reputation: 26238

What if you simply add a step with setNames as mentioned by dear @Ronak in an answer to another question? Am I missing somewhere?

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

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389135

You can use sapply to extract the first value of ID from each list and add it to the names.

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

Or with map_chr if you need it in tidyverse.

library(dplyr)
library(purrr)

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

The problem would be automatically solved if you use base::split

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

Upvotes: 3

Related Questions