[R] Accessing a list

Greg Snow 538280 at gmail.com
Thu Apr 26 00:02:59 CEST 2012


I believe that fortune(312) applies here.  As my current version of
fortunes does not show this I am guessing that it is in the
development version and so here is what fortune(312) will eventually
print (unless something changes or I got something wrong):

The problem here is that the $ notation is a magical shortcut and like
any other magic if used
incorrectly is likely to do the programmatic equivalent of turning
yourself into a toad.
—Greg Snow (in response to a user that wanted to access a column whose name is
stored in y via x$y rather than x[[y]])
R-help (February 2012)

On Tue, Apr 24, 2012 at 9:42 PM, Jim Silverton <jim.silverton at gmail.com> wrote:
> Hi,
> I have the following problem- I want to access a list whose elements are
> imp1, imp2, imp3 etc I tried theusing the paste comand in a for loop see
> the last for loop below. But I keep calling it df but df = imp1 (for the
> first run). Any ideas on how I can access the elements of the list?
>
> Isaac
>
>
>
> require(Amelia)
> library(Amelia)
> data.use <- read.csv("multiplecarol.CSV", header=T)
> names(data.use) = c("year", "dischargex1", "y", "pressurex2" , "windx3")
>
> ts <- c (c(1:12), c(1:12), c(1:12), c(1:12), c(1:12), c(1:12), c(1:12),
> c(1:6) )
> length(ts)
> data.use = cbind(ts, data.use)
>
> #a.out2 <- amelia(data.use, m = 1000, idvars = "year")
>
>
> n.times = 100
> a.out.time <- amelia(data.use, m = n.times, ts="ts", idvars="year",
> polytime=2)
>
> constant.col = dischargex1.col = pressurex2.col = windx3.col =
> rep(0,n.times)
>
> for (i in 1: n.times)
> {
> x = c("imp",i)
> df = paste(x, collapse = "")
> data1 = a.out.time[[1]]$df
> attach(data1)
> y = as.numeric(y)
> dischargex1 = as.numeric(dischargex1)
> pressurex2 = as.numeric(pressurex2)
> windx3 = as.numeric(windx3)
> multi.regress = lm(y~ dischargex1 + pressurex2 + windx3)
> constant.col[i] = as.numeric(multi.regress[[1]][1])
> dischargex1.col[i] = as.numeric(multi.regress[[1]][2])
> pressurex2.col[i] = as.numeric(multi.regress[[1]][3])
> windx3.col[i] = as.numeric(multi.regress[[1]][4])
> }
>
>
> --
> Thanks,
> Jim.
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com



More information about the R-help mailing list