The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

m.dr (70) [Avatar] Offline
#1
Hi -

I am working with the Airline data. I have read the data by year into a list that has the index of the years '1987'=data, '1988'=data, ....

So the data has several columns. And I can get to a column using:
sapply(data, '[', columnName). Now if columnName is 'dayOfMonth' and ...
If assign this to a variable as: x <- sapply(data, '[[', 'dayOfMonth')
The print (y)
I get a the list index and then the elements.
$`1987.dayOfMonth`
[1] 14 15 17 18 19 21 22 ...

Now I am trying to actually apply a factor to this column as in:
y <- factor(x)
However just indicating x - this does not take me to the list elements.
Instead I have to do: y <- factor(sapply(x, '[[', 1))

I cannot seem to do it as part of a for loop where I am going through all the years and applying a factor to dayOfMonth as I can never get to the list elements themselves.

I can get to the list elements to apply factor if I say y[[1]]$'1987.dayOfMonth'

But again this is not possible as I am doing this as part of a loop.
What is the best way to get to the elements directly so I can apply a factor to them. The code is getting too convoluted as I am having to do too many things to get to the factor when I think that there should be a way to do this more simply.

Also the data is part of an object which has a list that stores the years so I am already 3 levels in with the first sapply.

I hope this narrative is not too confusing - but just been quite stuck at this because every-time I try to apply the factor it says:

Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?

Thank you again for your book and help in this matter.