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

m.dr monosij.forums at gmail.com
Wed Nov 28 07:01:19 CET 2012


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.



More information about the R-help mailing list