2

I have searched posts on web to find a solution. But, I could not identify any. Therefore, I decided to ask your help. I have a list with data frames. I chose certain columns from each data frame and combine them. When I was combining data from two data frames, I wanted to add a column which includes the names of the list. But, I could not achieve this. Here are a sample data and what I have tried.

Sample data & 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 following ideal outcome with the new column, location. Please note that I trimmed the ideal outcome 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

One thing I tried was to use cbind() in the following way 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() may offer something, and that this can be done in a simple way. I could be wrong, though. Thank you very much for taking your time.

jazzurro
  • 23,179
  • 35
  • 66
  • 76
  • `plyr::ldply(ana)` should get you going. I am quite sure there is a nice answer somewhere on SO, also with `base` solutions. – Henrik Sep 10 '14 at 07:20
  • @Henrik Thank you for your support. I will further look for posts on SO meanwhile I seek a solution using `ldply`. – jazzurro Sep 10 '14 at 07:24
  • @jazzuro, [**Here**](http://stackoverflow.com/questions/15214472/use-object-names-within-a-list-in-lapply-ldply) or [**here**](http://stackoverflow.com/questions/22821526/create-a-data-frame-from-list-values/22821962#22821962) perhaps? – Henrik Sep 10 '14 at 07:30
  • @Roland Thank you very much for the solution. I was trying to avoid `for` loop. But, you taught me good stuff here. Thank you. – jazzurro Sep 10 '14 at 07:39
  • @Henrik Yes, the link gave me the answer. I was blind to `ldply`. Thank you for letting me know the link. I could not find it. – jazzurro Sep 10 '14 at 07:40

3 Answers3

1

You can do

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

to append the location column before calling rbind.fill.

flodel
  • 87,577
  • 21
  • 185
  • 223
  • Thank you very much for your support. I learned something new today. Since this was the first answer, I'd like to choose this as an answer. – jazzurro Sep 10 '14 at 07:47
0

Nothing wrong with a simple for loop:

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

And then use rbind.fill.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • Thank you for your support. This works great. I chose the answer of flodel since that came first. I hope you don't mind that. – jazzurro Sep 10 '14 at 07:48
0

Thank you very much for great support. The for loop and Map() solutions work absolutely fine. I learned a lot. At the same time, the link Henrik provided offered me the solution I was looking for. I, therefore, decided to leave the answer here. Once again, thank you for all 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
jazzurro
  • 23,179
  • 35
  • 66
  • 76