Skip to content

Accessing List within a List in a for Loop

4 messages · m.dr, Gerrit Eichner, David Winsemius

#
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.
#
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:

            
2 days later
#
Two things (at least) to consider if you post again.

1) This is a mailing list and Nabble is a cruel deception. It deludes  
you into thinking you are communicating effectively with your audience.

2) Much better to look at the results of str(object) than to use  
print(object)
On Nov 28, 2012, at 10:43 AM, m.dr wrote:

            
Not so "simple" as this I hope. Simple has a few meanings and this  
context-free posting is eliciting the interpretation of "simple" that  
you probably would like to avoid.