[R] Accessing List within a List in a for Loop

Gerrit Eichner Gerrit.Eichner at math.uni-giessen.de
Wed Nov 28 13:31:06 CET 2012


Hello, Mono,

your description of your problem is a bit "overwhelming". You might get an 
answer if you provide (simplified) reproducible code (maybe with example 
data).

I can only guess that -- instead of factor( x[[1]]) -- you maybe should 
use

factor( x[[1]][1])

in some place.

  Hth  --  Gerrit

On Tue, 27 Nov 2012, m.dr wrote:

> My question is regards accessing complex lists. I am new to R, but
> experienced in Java, Python, SQL.
>
> I am working with an object: Dataprocess and in it slot: data that is a
> list.
> Also I am creating generic functions to perform on the slot: data which is
> the list.
> I am working with the Airline dataset - so the data slot is a list of the
> years 1987, 1988, ..
> I am trying to perform factor and such operations on the column elements of
> each year.
> So - for purposes of this discussion we will refer to the column: dayOfWeek
> in each of the years.
>
> My issue is that while I get to the list I need I cannot directly access the
> list elements to apply the factor function to. I can print the list
> directly. I try to explain as follows:
> ...
> So the data has several columns. And in the method I can get to a column
> using:
> x <- list(lapply(this at data, '[[', ÿÿdayOfWeekÿÿ))
> print(x) OR print(x[1]) >> gives:
> [[1]]
> [[1]]$`1987`
> [1] 3 4 6 7 1 3 4 5 6 7 1 3 4 6 4 5 ...
>
> print(x[[1]]) >> gives:
> $`1987`
> [1] 3 4 6 7 1 3 4 5 6 7 1 3 4 6 4 5 ...
>
> If I do: z <- factor(x) OR z <- factor(x[1]) OR z <- factor(x[[1]]) I get
> Error in sort.list(y) : 'x' must be atomic for 'sort.list'
> Have you called 'sort' on a list?
> ...
> I have also tried:
> y <- list(sapply(this at data, '[', ÿÿdayOfWeekÿÿ))
> print(y) OR print (y[1]) >> gives:
> [[1]]
> [[1]]$`1987.dayOfWeek`
> [1] 3 4 6 7 1 3 4 5 6 7 1 3 4 6 4 5 ...
>
> print(y[[1]]) >> gives:
> $`1987.dayOfWeek`
> [1] 3 4 6 7 1 3 4 5 6 7 1 3 4 6 4 5 ...
>
> Again If I do: z <- factor(y) OR z <- factor(y[1]) OR z <- factor(y[[1]]) I
> get
> Error in sort.list(y) : 'x' must be atomic for 'sort.list'
> Have you called 'sort' on a list?
>
> I have tried accessing the list directly as y[[1]]$ÿÿ1987.dayOfWeekÿÿ which
> gives me the list elements.
> And then factor(y[[1]]$ÿÿdayOfWeekÿÿ) works fine!
>
> Gives me:
> [1] 3 4 6 7 1 3 4 5 6 7 1 3 4 6 4 5 ...
> Levels: 1 2 3 4 5 6 7
>
> But I do not have this flexibility as I am doing the lapply or sapply as
> part of a for loop.
> So the functions are really:
> x <- list(lapply(this at data, '[[', each))
> y <- list(sapply(this at data, '[', each))
>
> So it is not possible for me to access in a hard-coded manner as
> y[[1]]$ÿÿ1987.dayOfWeekÿÿ
> So I do not understand how to access the list elements directly to apply
> factor and not just the list.
> None of the accesses as x, x[1], x[[1]] OR y, y[1] or y[[1]] work.
> It gives me access to the list for print and such - but not to the list
> elements to apply factor.
> The only way to access the list elements directly is by
> y[[1]]$ÿÿ1987.dayOfWeekÿÿ and then I can factor.
>
> However I do not have this option as I am in a loop.
>
> I hope I have explained this clearly.
> If not please let me know and I can try again or post the code.
>
> Thank you for your help.
>
> Mono
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Accessing-List-within-a-List-in-a-for-Loop-tp4651086.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
>


More information about the R-help mailing list