[R] Defining multiple variables in a loop

Joshua Wiley jwiley.psych at gmail.com
Mon Jun 25 05:36:08 CEST 2012


Hi Taylor,

Even clearner would be:

countries <- lapply(1:20, function(i)  as.matrix(subset(OECDFiscal2,
Country == i)))

if you prefer to stick with the loop (up to you), I would instantiate
the list at size:


countries <- vector("list", 20)

for (i in 1:20) {
countries[[i]] = as.matrix(subset(OECDFiscal2, Country == i))
}

Cheers,

Josh

On Sun, Jun 24, 2012 at 8:31 PM, Taylor White
<taylorgentrywhite at gmail.com> wrote:
> Thank you gentlemen for pointing me in the right direction.
>
> This code worked nicely:
>
>
> countries <- list()
>
> for (i in 1:20) {
> countries[[i]] = as.matrix(subset(OECDFiscal2, Country == i))
> }
>
> Take care,
>
> Taylor
>
> On Sun, Jun 24, 2012 at 3:01 PM, R. Michael Weylandt
> <michael.weylandt at gmail.com> wrote:
>> To elaborate on what Bert said:
>>
>> Lists are a variable length data structure which can hold most
>> anything (even other lists) in them and are a great way to organize
>> the sort of data you're working with. You can think of them as
>> "generic vectors." You can assign them names and access/subset them by
>> names or by element number. Perhaps most usefully, instead of passing
>> all the vectors to a function which may need them, you can simply pass
>> the one list object. This will make things much easier to maintain in
>> the long run/
>>
>> Most everything complicated in R like data.frames or model objects are
>> internally implemented as lists (with various added features) and they
>> [lists] are exceptionally powerful. It will seem like more overhead
>> now than the lots-of-simple-vectors approach, but in the long run, it
>> will be most certainly worth it.
>>
>> Best,
>> Michael
>>
>> On Sun, Jun 24, 2012 at 4:30 PM, Bert Gunter <gunter.berton at gene.com> wrote:
>>> Standard response: Use lists instead.
>>>
>>> Read An Intro to R to learn about lists. In fact,read an An Intro to R,
>>> full stop ( if you have not already done so).
>>>
>>> Cheers,
>>> Bert
>>>
>>> On Sun, Jun 24, 2012 at 2:15 PM, Taylor White
>>> <taylorgentrywhite at gmail.com>wrote:
>>>
>>>> Good day,
>>>>
>>>> For lack of a better solution (or perhaps I am ignorant to something
>>>> more elegant), I have been bootstrapping panel data by hand so to
>>>> speak and I would like to know if there is a way to define multiple
>>>> variables in a loop using the loop variable.  I found a post (here:
>>>> https://stat.ethz.ch/pipermail/r-help/2002-October/026305.html ) that
>>>> discussed naming multiple variables but it didn't seem to allow me to
>>>> define the variables as something more useful.  I tried the code
>>>> immediately below (plus some variations) and it just didn't work.
>>>>
>>>> for (i in 1:20) {
>>>> assign(paste("country.", i, sep = "") <- subset(OECDFiscal2, Country == i)
>>>> }
>>>>
>>>>
>>>> I included some sample code from what I've been working on below so
>>>> one can see how it would be very useful to figure out how to define a
>>>> series of variables from cross sectional units from a panel dataset.
>>>>
>>>>
>>>> Any help would be much appreciated.
>>>>
>>>>
>>>> Thanks,
>>>>
>>>> Taylor White
>>>> UCLA
>>>>
>>>>
>>>>
>>>> ######Bootstrapping panel data by hand.  Create 4 variables from 3
>>>> subsets of the original data. Resample each variable and recombine
>>>> into one matrix.
>>>>
>>>>
>>>> plmcoef <- array(0, c(1000, 4)) #creates an empty array to store
>>>> regression coefficients
>>>> plmfixef <- array(0, c(1000, 3)) #creates an empty array to store
>>>> fixed effects intercepts from regressions
>>>>
>>>>
>>>> for (i in 1:1000) {
>>>> country1 <- as.data.frame(subset(OECDFiscal2, Country == 1))
>>>> country2 <- as.data.frame(subset(OECDFiscal2, Country == 2))
>>>> country3 <- as.data.frame(subset(OECDFiscal2, Country == 3))
>>>>
>>>> exp1 <- as.matrix(sample(country1$lagexpVSgdp, size =
>>>> (nrow(country1)), replace = T))
>>>> exp2 <- as.matrix(sample(country2$lagexpVSgdp, size =
>>>> (nrow(country2)), replace = T))
>>>> exp3 <- as.matrix(sample(country3$lagexpVSgdp, size =
>>>> (nrow(country3)), replace = T))
>>>>
>>>> tax1 <- as.matrix(sample(country1$lagtaxVSgdp1, size =
>>>> (nrow(country1)), replace = T))
>>>> tax2 <- as.matrix(sample(country2$lagtaxVSgdp1, size =
>>>> (nrow(country2)), replace = T))
>>>> tax3 <- as.matrix(sample(country3$lagtaxVSgdp1, size =
>>>> (nrow(country3)), replace = T))
>>>>
>>>> gdp1 <- as.matrix(sample(country1$yoygdpcapita, size =
>>>> (nrow(country1)), replace = T))
>>>> gdp2 <- as.matrix(sample(country2$yoygdpcapita, size =
>>>> (nrow(country2)), replace = T))
>>>> gdp3 <- as.matrix(sample(country3$yoygdpcapita, size =
>>>> (nrow(country3)), replace = T))
>>>>
>>>> unemployment1 <- as.matrix(sample(country1$lagunemployment, size =
>>>> (nrow(country1)), replace = T))
>>>> unemployment2 <- as.matrix(sample(country2$lagunemployment, size =
>>>> (nrow(country2)), replace = T))
>>>> unemployment3 <- as.matrix(sample(country3$lagunemployment, size =
>>>> (nrow(country3)), replace = T))
>>>>
>>>> country.year1 <- as.matrix(cbind(country1$Country, country1$Year2))
>>>> country.year2 <- as.matrix(cbind(country2$Country, country2$Year2))
>>>> country.year3 <- as.matrix(cbind(country3$Country, country3$Year2))
>>>>
>>>> country1.2 <- as.data.frame(cbind(country.year1, exp1, tax1, gdp1,
>>>> unemployment1))
>>>> country2.2 <- as.data.frame(cbind(country.year2, exp2, tax2, gdp2,
>>>> unemployment2))
>>>> country3.2 <- as.data.frame(cbind(country.year3, exp3, tax3, gdp3,
>>>> unemployment3))
>>>>
>>>> data <- as.data.frame(rbind(country1.2, country2.2, country3.2))
>>>>
>>>> OECDsamplepanel <- pdata.frame(data, index = NULL, drop = F)
>>>>
>>>> plm <- plm(V5 ~ lag(V6, 1) + V3 + V4 + V5, data = OECDSamplepanel,
>>>> model = "within")
>>>>
>>>> coefficients <- t(as.matrix(plm$coefficients))
>>>> fixef <- t(as.matrix(fixef(plm)))
>>>>
>>>> plmcoef[i, 1:4] = coefficients
>>>> plmfixef[i, 1:3] = fixef
>>>>
>>>> }
>>>>
>>>> ______________________________________________
>>>> 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.
>>>>
>>>
>>>
>>>
>>> --
>>>
>>> Bert Gunter
>>> Genentech Nonclinical Biostatistics
>>>
>>> Internal Contact Info:
>>> Phone: 467-7374
>>> Website:
>>> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>>>
>>>        [[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.
>
> ______________________________________________
> 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.



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list