Combining elements of different lengths in matching lists in R - r

I am trying to combine information from two corresponding lists, both of 4. The first list only contains 1 element for each of the 4 items, and is of the following structure:
List of 4
$ :'data.frame': 8640 obs. of 3 variables:
..$ x : num [1:8640] -108 -108 -108 -107 -107 ...
..$ y : num [1:8640] 25.9 25.9 25.9 25.9 25.9 ...
..$ layer: num [1:8640] 0 0 0 0 0 0 0 0 0 0 ...
$ :'data.frame': 20520 obs. of 3 variables:
..$ x : num [1:20520] -116 -116 -116 -115 -115 ...
..$ y : num [1:20520] 32.9 32.9 32.9 32.9 32.9 ...
..$ layer: num [1:20520] 0.002 0 0 0 0 ...
$ :'data.frame': 13500 obs. of 3 variables:
..$ x : num [1:13500] -112 -112 -112 -111 -111 ...
..$ y : num [1:13500] 29.9 29.9 29.9 29.9 29.9 ...
..$ layer: num [1:13500] 0.00583 0.01166 0.01749 0.01749 0.01749 ...
$ :'data.frame': 15300 obs. of 3 variables:
..$ x : num [1:15300] -117 -117 -117 -116 -116 ...
..$ y : num [1:15300] 31.9 31.9 31.9 31.9 31.9 ...
..$ layer: num [1:15300] 0 0 0 0 0 0 0 0 0 0 ...
I have another list that is also of 4, where I want to add the data in that list as 2 separate columns to the dataframes in their corresponding elements in the first list.
The structure of this second list is as follows:
List of 4
$ : chr [1:2] "green" "0.00689301"
$ : chr [1:2] "blue" "0.01291301"
$ : chr [1:2] "red" "0.02905452"
$ : chr [1:2] "black" "0.00879968"
Basically, I want a new list that has the following structure in each of the 4 members of the list:
List of 4
$ :'data.frame': 8640 obs. of 3 variables:
..$ x : num [1:8640] -108 -108 -108 -107 -107 ...
..$ y : num [1:8640] 25.9 25.9 25.9 25.9 25.9 ...
..$ layer: num [1:8640] 0 0 0 0 0 0 0 0 0 0 ...
..$ color: chr [1:8640] "green" "green" "green"...
..$ value: chr [1:8640] "0.00689301" "0.00689301" ...
$ :'data.frame': 20520 obs. of 3 variables:
..$ x : num [1:20520] -116 -116 -116 -115 -115 ...
..$ y : num [1:20520] 32.9 32.9 32.9 32.9 32.9 ...
..$ layer: num [1:20520] 0.002 0 0 0 0 ...
..$ color: chr [1:20520] "blue" "blue" "blue" ...
..$ value: chr [1:20520] "0.01291301" "0.01291301" ...
I have tried combining this information all at once using mapply() and c(), but that didn't give me the flexibility to change these new members from my second list from individual values into vectors of the same length as the corresponding new list.

We may use Map
Map(function(x, y) transform(x, color = y[1], value = y[2]), lst1, lst2)
# or another way is
Map(function(x, y) {x[c("color", "value")] <- as.list(y); x}, lst1, lst2)
Or with map2
library(purrr)
library(dplyr)
map2(lst1, lst2, ~ .x %>%
mutate(color = .y[1], value = .y[2]))

Related

Melt not working when converting from Reshape to Reshape 2

Melt not working when upgrading from Reshape to Reshape 2
I have a large list of values. Here is the summary (lots of columns):
List of 46
$ Date: Date[1:9], format: "2011-03-04" ...
$ 1 : num [1:9] 20278 19493 20587 24679 55708 ...
$ 2 : num [1:9] 24029 25317 25103 28871 79423 ...
$ 3 : num [1:9] 6657 7025 6603 8105 17883 ...
$ 4 : num [1:9] 29684 27555 28956 31504 73638 ...
$ 5 : num [1:9] 9572 8759 9947 11173 22341 ...
$ 6 : num [1:9] 18935 20168 22963 24387 58640 ...
$ 7 : num [1:9] 8299 8297 10484 10211 19277 ...
$ 8 : num [1:9] 14365 13691 13906 17149 38364 ...
$ 9 : num [1:9] 10333 10899 9708 11297 24100 ...
$ 10 : num [1:9] 33647 33455 35327 49031 128927 ...
$ 11 : num [1:9] 15090 16105 16343 18624 53809 ...
$ 12 : num [1:9] 17971 16408 15911 18350 44048 ...
$ 13 : num [1:9] 36820 44024 52026 62491 142186 ...
$ 14 : num [1:9] 27036 33240 39248 53035 148606 ...
$ 15 : num [1:9] 11490 11704 12587 17840 50201 ...
$ 16 : num [1:9] 11016 11768 13711 13323 21258 ...
$ 17 : num [1:9] 19792 18734 20477 30433 66028 ...
$ 18 : num [1:9] 19920 20316 21285 29360 88008 ...
$ 19 : num [1:9] 17046 19281 19610 30376 80302 ...
$ 20 : num [1:9] 32886 38971 44672 53278 141423 ...
$ 21 : num [1:9] 11324 13211 13123 15510 32014 ...
$ 22 : num [1:9] 21416 23530 25978 37096 94035 ...
$ 23 : num [1:9] 29527 33310 32701 42628 112442 ...
$ 24 : num [1:9] 19479 19181 20525 25210 69559 ...
$ 25 : num [1:9] 20727 20620 22190 29052 59528 ...
$ 26 : num [1:9] 16056 15122 15240 17327 39292 ...
$ 27 : num [1:9] 19020 28919 29659 43806 94475 ...
$ 28 : num [1:9] 19041 15803 15940 20319 49065 ...
$ 29 : num [1:9] 15775 15080 17841 21492 49891 ...
$ 30 : num [1:9] 9554 10395 9605 11513 13558 ...
$ 31 : num [1:9] 15322 16603 16348 17228 32973 ...
$ 32 : num [1:9] 19752 21591 21272 24639 52204 ...
$ 33 : num [1:9] 2017 2109 1944 1899 2224 ...
$ 34 : num [1:9] 18797 18496 17514 20066 39702 ...
$ 35 : num [1:9] 14306 13489 14507 18560 51028 ...
$ 36 : num [1:9] 2247 2558 2232 2401 2931 ...
$ 37 : num [1:9] 10971 10779 10272 11788 17386 ...
$ 38 : num [1:9] 6241 6414 6024 6291 8257 ...
$ 39 : num [1:9] 16933 18888 20160 25847 60786 ...
$ 40 : num [1:9] 18254 17638 17956 20265 43778 ...
$ 41 : num [1:9] 18249 19955 20016 25647 53012 ...
$ 42 : num [1:9] 9917 10655 10194 10354 15472 ...
$ 43 : num [1:9] 6561 6903 6941 6174 14034 ...
$ 44 : num [1:9] 5857 5968 6283 7645 9861 ...
$ 45 : num [1:9] 17185 18197 19508 26187 67014 ...
- attr(*, "row.names")= int [1:9] 1 2 3 4 5 6 7 8 9
- attr(*, "idvars")= chr "Date"
- attr(*, "rdimnames")=List of 2
..$ :'data.frame': 9 obs. of 1 variable:
.. ..$ Date: Date[1:9], format: "2011-03-04" ...
..$ :'data.frame': 45 obs. of 1 variable:
.. ..$ Store: num [1:45] 1 2 3 4 5 6 7 8 9 10 ...
'data.frame': 405 obs. of 3 variables:
$ Date : Date, format: "2011-03-04" ...
$ value: num 20278 19493 20587 24679 55708 ...
$ Store: num 1 1 1 1 1 1 1 1 1 2 ...
With the original reshape library I am able to melt it down without issue:
'data.frame': 405 obs. of 3 variables:
$ Date : Date, format: "2011-03-04" ...
$ value: num 20278 19493 20587 24679 55708 ...
$ Store: num 1 1 1 1 1 1 1 1 1 2 ...
However, when I try to use melt from Reshape2, I get the following warning and error:
attributes are not identical across measure variables; they will be dropped
Error: `by` must be supplied when `x` and `y` have no common variables.
What happened here between versions here? Any suggestions for fixing? I'm stuck using Reshape2 for this. Thanks!

How to concatenate all vlaues in a column which is a list of data frames R

structure of data frame
> str(df)
'data.frame': 459 obs. of 6 variables:
$ Source : chr "Mumbai" "Mumbai" "Bangalore" "Bangalore" ...
$ Destination: chr "Bangalore" "Bangalore" "Chennai" "Cochin" ...
$ src_loc :'data.frame': 459 obs. of 2 variables:
..$ lon: num 72.9 72.9 77.6 77.6 73.9 ...
..$ lat: num 19.1 19.1 13 13 18.5 ...
$ dest_loc :'data.frame': 459 obs. of 2 variables:
..$ lon: num 77.6 77.6 80.3 76.3 78.5 ...
..$ lat: num 12.97 12.97 13.08 9.93 17.39 ...
$ route_line:List of 459
..$ :'data.frame': 219 obs. of 2 variables:
.. ..$ lat: num 19.1 19.1 19.1 19.1 19.1 ...
.. ..$ lon: num 72.9 72.9 72.9 72.9 73 ...
..$ :'data.frame': 219 obs. of 2 variables:
.. ..$ lat: num 19.1 19.1 19.1 19.1 19.1 ...
.. ..$ lon: num 72.9 72.9 72.9 72.9 73 ...
..$ :'data.frame': 244 obs. of 2 variables:
.. ..$ lat: num 13 13 13 13 13 ...
.. ..$ lon: num 77.6 77.6 77.6 77.6 77.6 ...
..$ :'data.frame': 228 obs. of 2 variables:
.. ..$ lat: num 13 13 13 12.9 12.9 ...
.. ..$ lon: num 77.6 77.6 77.6 77.6 77.6 ...
..$ :'data.frame': 232 obs. of 2 variables:
.. ..$ lat: num 18.5 18.5 18.5 18.5 18.5 ...
.. ..$ lon: num 73.9 73.9 73.9 73.9 73.9 ...
..$ :'data.frame': 234 obs. of 2 variables:
.. ..$ lat: num 15.4 15.4 15.4 15.4 15.4 ...
.. ..$ lon: num 75.1 75.1 75.1 75.1 75.1 ...
..$ :'data.frame': 218 obs. of 2 variables:
.. ..$ lat: num 17.4 17.4 17.4 17.5 17.5 ...
.. ..$ lon: num 78.5 78.5 78.5 78.5 78.5 ...
so on..
> df$route_line[[1]] #gives a data frame
lat lon
1 19.07597 72.87765
2 19.06575 72.89918
3 19.06331 72.91443
4 19.05159 72.93661
5 19.06758 72.98437
6 19.06653 73.02000
7 19.04099 73.02868
8 19.02309 73.04452
9 19.03844 73.07676
10 18.99688 73.13215
11 18.98191 73.14718
12 18.96049 73.15789
13 18.94201 73.15694
14 18.92484 73.16662
15 18.89439 73.20433
16 18.84075 73.24026
17 18.81434 73.27669
18 18.79409 73.29148
19 18.77373 73.32182
20 18.77023 73.33760
21 18.76414 73.34698
22 18.77114 73.36076
23 18.76580 73.35765
24 18.77090 73.36348
25 18.75822 73.37283
26 18.76368 73.38653
27 18.76939 73.40145
28 18.76301 73.41848
29 18.75766 73.42920
30 18.73973 73.42921
I want to create a new column (with name route_str) which contains the string obtained by concatenating all latitudes and longitudes in the above obtained data frame for every row in df
For example,
> df$route_str[1] #should give
[1] "19.07597 72.87765, 19.06575 72.89918, 19.06331 72.91443,19.05159 72.93661..." so on till 30
I tried this
> fun <- function(ip)
+ {
+ a <- ip[[1]]
+ a[3] <- paste(a[1],a[2]," ")
+ op <- paste(a[3],collapse = ",")
+ return(op)
+ }
> df$route_str <- lapply(df$route_line,fun)
But the output I get is
> unique_routes$route_str[1]
[[1]]
[1] "19.0759696960449 19.0657501220703 "
I tried to create reproducible data using following code but the structure isn't the same
df <- data.frame(src=c("chennai","Mumbai","Bangalore"),dest=c("Mumbai","Bangalore","Mumbai"),route=list(list(lat=c(19,20,21),lon=c(72,73,74)),data.frame(lat=c(19,20,21),lon=c(72,73,74)),data.frame(lat=c(19,20,21),lon=c(72,73,74))))
But the structure of above created data is as follows
> str(df)
'data.frame': 3 obs. of 8 variables:
$ src : Factor w/ 3 levels "Bangalore","chennai",..: 2 3 1
$ dest : Factor w/ 2 levels "Bangalore","Mumbai": 2 1 2
$ route.lat : num 19 20 21
$ route.lon : num 72 73 74
$ route.lat.1: num 19 20 21
$ route.lon.1: num 72 73 74
$ route.lat.2: num 19 20 21
$ route.lon.2: num 72 73 74
I am using R version 3.3.1 on windows 10 pls help!
EDIT:
This is how I ended up with that complicated data frame
Initial Data frame was like this
> df <- data.frame(source=c("chennai","Mumbai","Bangalore"),destination=c("Mumbai","Bangalore","Mumbai"))
> df
source destination
1 chennai Mumbai
2 Mumbai Bangalore
3 Bangalore Mumbai
I want to have a column containing single string with all the way-points(lat lon) between source and destination separated by a comma
I used googleway package to get waypoints
> library(googleway)
> res <- function(src,dest,key) #key is google maps API key
+ {
+ polylinex <- google_directions(origin = src,destination = dest,key = key)
+ return(polylinex$routes$overview_polyline$points)
+ }
> df$source <- as.character(df$source)
> df$destination <- as.character(df$destination)
> df$x <- mapply(res,df$source,df$destination,key)
> df$route_line <- lapply(df$x,function(y) googleway::decode_pl(y))
> df <- df[,!(names(df)=="x")]
> str(df)
'data.frame': 3 obs. of 3 variables:
$ source : chr "chennai" "Mumbai" "Bangalore"
$ destination: chr "Mumbai" "Bangalore" "Mumbai"
$ route_line :List of 3
..$ :'data.frame': 219 obs. of 2 variables:
.. ..$ lat: num 13.1 13.1 13.1 13.1 13.1 ...
.. ..$ lon: num 80.3 80.2 80.2 80.2 80.2 ...
..$ :'data.frame': 219 obs. of 2 variables:
.. ..$ lat: num 19.1 19.1 19.1 19.1 19.1 ...
.. ..$ lon: num 72.9 72.9 72.9 72.9 73 ...
..$ :'data.frame': 218 obs. of 2 variables:
.. ..$ lat: num 13 13 13 13 13 ...
.. ..$ lon: num 77.6 77.6 77.6 77.6 77.5 ...
A slight modification to your lapply into an sapply, and altering the paste sequence slightly will get you want you want
df$route_str <- sapply(df$x, function(y){
df_coords <- decode_pl(y)
paste0(t(sapply(df_coords, paste0)), collapse = ",")
})
str(df)
'data.frame': 3 obs. of 4 variables:
$ source : chr "chennai" "Mumbai" "Bangalore"
$ destination: chr "Mumbai" "Bangalore" "Mumbai"
$ x : chr "weznA{z|hNjrAlkDue#vsDtVnhD|dAnkErSbdI~kGzmRtmLjrNldI|iWnjBbuDf^duJgPzqNsiCtaIyLpnOyXzrKe{AvaG|JxpF~VpkCga#tkG_sBp|Cev#fvDpI|gF"| __truncated__ "ywlsBi|x{Lz~#qeCfNi~AfhAsiC}bBoiHpEu}Er~Cgu#znB_bB}~AohEvbGeyIp|A}|AzdC}aAnrB|DhjBo{#h}DujFfnIq_F`dDubFp}Bm{Af~Bs|DzTsaB`e#uy#w"| __truncated__ "oodnA}drxMkcAhKggApm#s}A|uAey#|rAi~BdjF{fDpaLgxB||F}`DvxE{sDdmDgkGthKmlK|vJmgIbzJa`BrjCssC|aBw`Dvw#osBrkCutNpbIigD|sCk`Ft_C}iPv"| __truncated__
$ route_str : chr "13.0826797485352,80.2706985473633,13.0693397521973,80.2431106567383,13.0755300521851,80.2141876220703,13.0717391967773,80.18706"| __truncated__ "19.0759696960449,72.8776473999023,19.0657501220703,72.8991775512695,19.0633087158203,72.9144287109375,19.0515899658203,72.93660"| __truncated__ "12.9715995788574,77.5945510864258,12.9825401306152,77.5925750732422,12.9940996170044,77.5851287841797,13.0092391967773,77.57122"| __truncated__
Note: I'm the googleway author, thanks for using the package

How Can I Quickly Inspect Built-in Data Sets (PSA)?

One of the best ways to make a question reproducible is to use one of the built in data sets. Using data(), however, is frustrating because no information about the structure of the data set is provided.
How can I quickly view the structure of available data sets?
The following function may help:
dataStr <- function(fun=function(x) TRUE)
str(
Filter(
fun,
Filter(
Negate(is.null),
mget(data()$results[, "Item"], inh=T, ifn=list(NULL))
) ) )
It accepts a filtering function, applies it to all the data sets, and prints out the structure of the matching data sets. For example, if we're looking for matrices:
> dataStr(is.matrix)
List of 8
$ WorldPhones : num [1:7, 1:7] 45939 60423 64721 68484 71799 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:7] "1951" "1956" "1957" "1958" ...
.. ..$ : chr [1:7] "N.Amer" "Europe" "Asia" "S.Amer" ...
$ occupationalStatus : 'table' int [1:8, 1:8] 50 16 12 11 2 12 0 0 19 40 ...
..- attr(*, "dimnames")=List of 2
.. ..$ origin : chr [1:8] "1" "2" "3" "4" ...
.. ..$ destination: chr [1:8] "1" "2" "3" "4" ...
$ volcano : num [1:87, 1:61] 100 101 102 103 104 105 105 106 107 108 ...
--- 5 entries omitted ---
Or for data frames (also omitting entries):
> dataStr(is.data.frame)
List of 42
$ BOD :'data.frame': 6 obs. of 2 variables:
..$ Time : num [1:6] 1 2 3 4 5 7
..$ demand: num [1:6] 8.3 10.3 19 16 15.6 19.8
..- attr(*, "reference")= chr "A1.4, p. 270"
$ CO2 :Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame': 84 obs. of 5 variables:
..$ Plant : Ord.factor w/ 12 levels "Qn1"<"Qn2"<"Qn3"<..: 1 1 1 1 1 1 1 2 2 2 ...
..$ Type : Factor w/ 2 levels "Quebec","Mississippi": 1 1 1 1 1 1 1 1 1 1 ...
..$ Treatment: Factor w/ 2 levels "nonchilled","chilled": 1 1 1 1 1 1 1 1 1 1 ...
..$ conc : num [1:84] 95 175 250 350 500 675 1000 95 175 250 ...
..$ uptake : num [1:84] 16 30.4 34.8 37.2 35.3 39.2 39.7 13.6 27.3 37.1 ...
--- 40 entries omitted ---
Or even for simple vectors:
> dataStr(function(x) is.atomic(x) && is.vector(x) && !is.ts(x))
List of 4
$ euro : Named num [1:11] 13.76 40.34 1.96 166.39 5.95 ...
..- attr(*, "names")= chr [1:11] "ATS" "BEF" "DEM" "ESP" ...
$ islands: Named num [1:48] 11506 5500 16988 2968 16 ...
..- attr(*, "names")= chr [1:48] "Africa" "Antarctica" "Asia" "Australia" ...
$ precip : Named num [1:70] 67 54.7 7 48.5 14 17.2 20.7 13 43.4 40.2 ...
..- attr(*, "names")= chr [1:70] "Mobile" "Juneau" "Phoenix" "Little Rock" ...
$ rivers : num [1:141] 735 320 325 392 524 ...

Plot NYC Citi Bike Data, Lat and Long Not Displayed on Map

I'm trying to plot the NYC citi bike station data on top of a map of NYC.
I downloaded zipcode data from here:
Here is what I've done:
> bike.loc<-bike.train
> nyc.zip<-readShapePoly("nyc_zipcta.shp")
> coordinates(bike.loc)<-c("start.station.id","end.station.id")
> class(bike.loc)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"
> str(bike.loc)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
..# data :'data.frame': 150000 obs. of 15 variables:
.. ..$ tripduration : int [1:150000] 970 711 549 883 88 535 975 307 218 542 ...
.. ..$ starttime : Factor w/ 41673 levels "7/1/13 0:00",..: 8738 12152 27602 11984 9651 21822 24531 13946 17666 20150 ...
.. ..$ stoptime : Factor w/ 41752 levels "7/1/13 0:04",..: 8774 12769 27646 12006 9647 21866 24585 13961 17689 18964 ...
.. ..$ start.station.name : Factor w/ 329 levels "1 Ave & E 15 St",..: 9 277 298 321 267 329 71 197 266 182 ...
.. ..$ start.station.latitude : num [1:150000] 40.7 40.7 40.8 40.7 40.7 ...
.. ..$ start.station.longitude: num [1:150000] -74 -74 -74 -74 -74 ...
.. ..$ end.station.name : Factor w/ 329 levels "1 Ave & E 15 St",..: 193 124 6 159 267 73 76 73 116 227 ...
.. ..$ end.station.latitude : num [1:150000] 40.7 40.7 40.8 40.7 40.7 ...
.. ..$ end.station.longitude : num [1:150000] -74 -74 -74 -74 -74 ...
.. ..$ bikeid : int [1:150000] 15301 17873 17596 15864 19005 17230 15476 19805 18494 18104 ...
.. ..$ usertype : Factor w/ 2 levels "Customer","Subscriber": 2 2 2 2 1 2 1 2 2 2 ...
.. ..$ birth.year : Factor w/ 79 levels "\\N","1899","1900",..: 67 37 70 67 1 55 1 45 73 63 ...
.. ..$ gender : int [1:150000] 1 1 1 2 0 1 0 1 1 1 ...
.. ..$ hour : int [1:150000] 19 1 2 8 12 14 15 17 11 9 ...
.. ..$ day : int [1:150000] 15 18 28 17 16 24 26 19 21 22 ...
..# coords.nrs : int [1:2] 4 8
..# coords : num [1:150000, 1:2] 528 466 495 328 212 430 358 323 482 406 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "start.station.id" "end.station.id"
..# bbox : num [1:2, 1:2] 72 72 3002 3002
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "start.station.id" "end.station.id"
.. .. ..$ : chr [1:2] "min" "max"
..# proj4string:Formal class 'CRS' [package "sp"] with 1 slots
.. .. ..# projargs: chr NA
I can produce a plot of NYC
plot(nyc.zip)
But I cannot plot the coordinates on top.
plot(bike.loc, add=T, col= "red", pch=15)
I've tried:
EPSG <- make_EPSG()
NY <- with(EPSG,EPSG[grepl("New York",note) & code==2263,]$prj4)
Based on this post, but haven't got it to work.
How do I get the lat/longs plotted over the map?

How can I split a multiply imputed dataset created in Amelia?

I have imputed missing values using Amelia thereby creating 5 multiply imputed datasets. Now, I would like to split this multi-dataset, e.g. one set for year => 1990 and one set for year =<1990. Any ideas how I can do so? Many thanks!
data(freetrade)
freetrade$year #splitting variable
#Imputation of missing data
a.out <- amelia(freetrade, m=5, ts="year", cs="country")
#split of created dataset?
Amelia returns an object that contains a list of dataframes (for each imputations). You can see the structure of this object with str().
> library(Amelia)
> data(freetrade)
>
> a.out <- amelia(freetrade, m=5, ts="year", cs="country")
-- Imputation 1 --
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
-- Imputation 2 --
1 2 3 4 5 6 7 8 9 10 11 12 13
-- Imputation 3 --
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
-- Imputation 4 --
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
-- Imputation 5 --
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> str(a.out)
List of 12
$ imputations:List of 5
..$ imp1:'data.frame': 171 obs. of 10 variables:
.. ..$ year : int [1:171] 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 ...
.. ..$ country : chr [1:171] "SriLanka" "SriLanka" "SriLanka" "SriLanka" ...
.. ..$ tariff : num [1:171] 30.6 22.4 41.3 26.8 31 ...
.. ..$ polity : num [1:171] 6 5 5 5 5 5 5 5 5 5 ...
.. ..$ pop : num [1:171] 14988000 15189000 15417000 15599000 15837000 ...
.. ..$ gdp.pc : num [1:171] 461 474 489 508 526 ...
.. ..$ intresmi: num [1:171] 1.94 1.96 1.66 2.8 2.26 ...
.. ..$ signed : num [1:171] 0 0 1 0 0 0 0 1 0 0 ...
.. ..$ fiveop : num [1:171] 12.4 12.5 12.3 12.3 12.3 ...
.. ..$ usheg : num [1:171] 0.259 0.256 0.266 0.299 0.295 ...
..$ imp2:'data.frame': 171 obs. of 10 variables:
.. ..$ year : int [1:171] 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 ...
.. ..$ country : chr [1:171] "SriLanka" "SriLanka" "SriLanka" "SriLanka" ...
.. ..$ tariff : num [1:171] 33.6 59.7 41.3 18.2 31 ...
.. ..$ polity : num [1:171] 6 5 5 5 5 5 5 5 5 5 ...
.. ..$ pop : num [1:171] 14988000 15189000 15417000 15599000 15837000 ...
.. ..$ gdp.pc : num [1:171] 461 474 489 508 526 ...
.. ..$ intresmi: num [1:171] 1.94 1.96 1.66 2.8 2.26 ...
.. ..$ signed : num [1:171] 0 0 1 0 0 0 0 1 0 0 ...
.. ..$ fiveop : num [1:171] 12.4 12.5 12.3 12.3 12.3 ...
.. ..$ usheg : num [1:171] 0.259 0.256 0.266 0.299 0.295 ...
..$ imp3:'data.frame': 171 obs. of 10 variables:
.. ..$ year : int [1:171] 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 ...
.. ..$ country : chr [1:171] "SriLanka" "SriLanka" "SriLanka" "SriLanka" ...
.. ..$ tariff : num [1:171] 48.5 32.9 41.3 47.2 31 ...
.. ..$ polity : num [1:171] 6 5 5 5 5 5 5 5 5 5 ...
.. ..$ pop : num [1:171] 14988000 15189000 15417000 15599000 15837000 ...
.. ..$ gdp.pc : num [1:171] 461 474 489 508 526 ...
.. ..$ intresmi: num [1:171] 1.94 1.96 1.66 2.8 2.26 ...
.. ..$ signed : num [1:171] 0 0 1 0 0 0 0 1 0 0 ...
.. ..$ fiveop : num [1:171] 12.4 12.5 12.3 12.3 12.3 ...
.. ..$ usheg : num [1:171] 0.259 0.256 0.266 0.299 0.295 ...
..$ imp4:'data.frame': 171 obs. of 10 variables:
.. ..$ year : int [1:171] 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 ...
.. ..$ country : chr [1:171] "SriLanka" "SriLanka" "SriLanka" "SriLanka" ...
.. ..$ tariff : num [1:171] 18.4 45.5 41.3 16.9 31 ...
.. ..$ polity : num [1:171] 6 5 5 5 5 5 5 5 5 5 ...
.. ..$ pop : num [1:171] 14988000 15189000 15417000 15599000 15837000 ...
.. ..$ gdp.pc : num [1:171] 461 474 489 508 526 ...
.. ..$ intresmi: num [1:171] 1.94 1.96 1.66 2.8 2.26 ...
.. ..$ signed : num [1:171] 0 0 1 0 0 0 0 1 0 0 ...
.. ..$ fiveop : num [1:171] 12.4 12.5 12.3 12.3 12.3 ...
.. ..$ usheg : num [1:171] 0.259 0.256 0.266 0.299 0.295 ...
..$ imp5:'data.frame': 171 obs. of 10 variables:
.. ..$ year : int [1:171] 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 ...
.. ..$ country : chr [1:171] "SriLanka" "SriLanka" "SriLanka" "SriLanka" ...
.. ..$ tariff : num [1:171] 15.3 44.4 41.3 40.1 31 ...
.. ..$ polity : num [1:171] 6 5 5 5 5 5 5 5 5 5 ...
.. ..$ pop : num [1:171] 14988000 15189000 15417000 15599000 15837000 ...
.. ..$ gdp.pc : num [1:171] 461 474 489 508 526 ...
.. ..$ intresmi: num [1:171] 1.94 1.96 1.66 2.8 2.26 ...
.. ..$ signed : num [1:171] 0 0 1 0 0 0 0 1 0 0 ...
.. ..$ fiveop : num [1:171] 12.4 12.5 12.3 12.3 12.3 ...
.. ..$ usheg : num [1:171] 0.259 0.256 0.266 0.299 0.295 ...
..- attr(*, "class")= chr [1:2] "mi" "list"
$ m : num 5
$ missMatrix : logi [1:171, 1:10] FALSE FALSE FALSE FALSE FALSE FALSE ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr [1:10] "year" "country" "tariff" "polity" ...
$ overvalues : NULL
$ theta : num [1:9, 1:9, 1:5] -1 -0.08456 -0.03404 -0.00193 0.06483 ...
$ mu : num [1:8, 1:5] -0.08456 -0.03404 -0.00193 0.06483 -0.11178 ...
$ covMatrices: num [1:8, 1:8, 1:5] 0.7881 -0.1869 -0.0531 0.2121 -0.0819 ...
$ code : num 1
$ message : chr "Normal EM convergence."
$ iterHist :List of 5
..$ : num [1:15, 1:3] 44 34 25 28 26 25 24 22 20 14 ...
..$ : num [1:13, 1:3] 44 27 24 22 22 21 18 17 14 11 ...
..$ : num [1:19, 1:3] 44 34 29 27 26 26 25 24 23 21 ...
..$ : num [1:15, 1:3] 44 34 27 28 23 24 23 23 19 19 ...
..$ : num [1:20, 1:3] 44 32 30 27 24 23 23 23 23 21 ...
$ arguments :List of 22
..$ idvars : NULL
..$ logs : NULL
..$ ts : num 1
..$ cs : num 2
..$ empri : NULL
..$ tolerance : num 1e-04
..$ polytime : NULL
..$ splinetime : NULL
..$ lags : NULL
..$ leads : NULL
..$ intercs : logi FALSE
..$ sqrts : NULL
..$ lgstc : NULL
..$ noms : NULL
..$ ords : NULL
..$ priors : NULL
..$ autopri : num 0.05
..$ bounds : NULL
..$ max.resample: num 100
..$ startvals : num 0
..$ overimp : NULL
..$ emburn : num [1:2] 0 0
..- attr(*, "class")= chr [1:2] "ameliaArgs" "list"
$ orig.vars : chr [1:10] "year" "country" "tariff" "polity" ...
- attr(*, "class")= chr "amelia"
From here you can see that the the "imputations" element of your a.out object contains your data frames, so you can reference each of your imputations from there. For example a.out$imputations[[1]]$year will give you the years from your first imputation. If you like to do that across each imputation then you can do so using an apply function or loop. To illustrate this, consider:
> sapply(a.out$imputations,function(x) head(x$year))
imp1 imp2 imp3 imp4 imp5
[1,] 1981 1981 1981 1981 1981
[2,] 1982 1982 1982 1982 1982
[3,] 1983 1983 1983 1983 1983
[4,] 1984 1984 1984 1984 1984
[5,] 1985 1985 1985 1985 1985
[6,] 1986 1986 1986 1986 1986
EDIT: I just re-read your question and I saw that you're actually looking for something more specific. You can take what's above an apply it to make subsets of each each data frame doing something like lapply(a.out$imputations,function(x) x[x$year > 1990,]). I'm not sure how you would like to combine these imputed datasets (split by years great than/less than 1990), but if you just want to append all rows together rbind() will do the trick (if not let me know how you'd like to and I can probably recommend a solution):
> df1 <- do.call(rbind,lapply(a.out$imputations,function(x) x[x$year > 1990,]))
> df2 <- do.call(rbind,lapply(a.out$imputations,function(x) x[x$year < 1990,]))
> head(df1)
year country tariff polity pop gdp.pc intresmi signed fiveop usheg
imp1.11 1991 SriLanka 26.9000 5 17247000 597.6987 2.285213 1.000000 12.8 0.2589872
imp1.12 1992 SriLanka 25.0000 5 17405000 618.3329 2.877877 0.515665 13.1 0.2623017
imp1.13 1993 SriLanka 24.2000 5 17628420 652.6205 4.280361 0.000000 13.2 0.2812928
imp1.14 1994 SriLanka 26.0000 5 17865000 680.0408 4.389912 0.000000 13.2 0.2783585
imp1.15 1995 SriLanka 20.0000 5 18112000 707.6591 3.995919 0.000000 13.2 0.2627195
imp1.16 1996 SriLanka 20.5646 5 18300000 727.0039 3.676763 0.000000 13.2 0.2681700
> head(df2)
year country tariff polity pop gdp.pc intresmi signed fiveop usheg
imp1.1 1981 SriLanka 30.56693 6 14988000 461.0236 1.937347 0 12.4 0.2593112
imp1.2 1982 SriLanka 22.39382 5 15189000 473.7634 1.964430 0 12.5 0.2558008
imp1.3 1983 SriLanka 41.30000 5 15417000 489.2266 1.663936 1 12.3 0.2655022
imp1.4 1984 SriLanka 26.81580 5 15599000 508.1739 2.797462 0 12.3 0.2988009
imp1.5 1985 SriLanka 31.00000 5 15837000 525.5609 2.259116 0 12.3 0.2952431
imp1.6 1986 SriLanka 17.76314 5 16117000 538.9237 1.832549 0 12.5 0.2886563

Resources