How to create a new column with names in a list

I searched the posts online to find a solution. But I could not determine. So I decided to ask for help. I have a list with data frames. I selected specific columns from each data frame and merged them. When I was collecting data from two data frames, I wanted to add a column that includes list names. But I could not achieve this. Here is sample data and what I tried.

Sample data and my attempt

### 1st dataframe time <- seq(as.Date("2014-09-01"), by = "day", length.out = 12) temperature <- sample(c(15:26), replace = TRUE) weather <- sample(c("clear", "cloudy", "rain"), size = 12, replace = TRUE) rome <- data.frame(time, temperature, weather, stringsAsFactors = F) ### 2nd dataframe time <- seq(as.Date("2014-09-01"), by = "day", length.out = 12) temperature <- sample(c(12:23), replace = TRUE) weather <- sample(c("clear", "cloudy", "rain"), size = 12, replace = TRUE) paris <- data.frame(time, temperature, weather, stringsAsFactors = F) ### Assign names to each data frame and create a list ana <- list(rome = rome, paris = paris) #Here are a bit of data. #> ana #$rome # time temperature weather #1 2014-09-01 19 cloudy #2 2014-09-02 21 cloudy #3 2014-09-03 17 clear #$paris # time temperature weather #1 2014-09-01 18 clear #2 2014-09-02 12 cloudy #3 2014-09-03 17 cloudy ### Select 1st and 2nd column from each data frame in the list and ### combine them. rbind.fill(lapply(ana, `[`, 1:2)) 

I wanted to add something here to create the next perfect result with a new column, location. Please note that I trimmed the perfect result to save space.

  time temperature location 1 2014-09-01 19 rome 2 2014-09-02 21 rome 3 2014-09-03 17 rome 13 2014-09-01 18 paris 14 2014-09-02 12 paris 15 2014-09-03 17 paris 

I tried using cbind() as follows, although I knew this would not work.

 lapply(ana, function(x) cbind(x, new = names(ana))) #$rome # time temperature new #1 2014-09-01 19 rome #2 2014-09-02 21 paris #3 2014-09-03 17 rome # #$paris # time temperature new #1 2014-09-01 18 rome #2 2014-09-02 12 paris #3 2014-09-03 17 rome 

I have the feeling that setNames() has something to offer, and this can be done in a simple way. Although I could be wrong. Thank you so much for taking the time.

+2
source share
3 answers

You can do

 ana <- Map(cbind, ana, location = names(ana)) 

add a location column before calling rbind.fill .

+1
source

Nothing wrong with a simple for loop:

 for (i in names(ana)) ana[[i]]$location <- i 

And then use rbind.fill .

0
source

Thanks so much for the great support. The for and Map() modes work absolutely fine. I learned a lot. At the same time, the link provided by Henrik offered me the solution I was looking for. So I decided to leave an answer here. Thank you again for your support.

 ldply(ana, rbind) .id time temperature weather 1 rome 2014-09-01 19 cloudy 2 rome 2014-09-02 21 cloudy 3 rome 2014-09-03 17 clear 13 paris 2014-09-01 18 clear 14 paris 2014-09-02 12 cloudy 15 paris 2014-09-03 17 cloudy 
0
source

Source: https://habr.com/ru/post/1203688/


All Articles