I have a list called tst , reproducible with this dput output below.
structure(list(CAF = structure(list(word = "CAF", freq = structure(list(
StartDate = structure(1:5, .Label = c("2004-01-04 - 2004-01-10",
"2004-01-11 - 2004-01-17", "2004-01-18 - 2004-01-24", "2004-01-25 - 2004-01-31",
"2004-02-01 - 2004-02-07"), class = "factor"), RelFreq = c(23L,
24L, 26L, 27L, 26L)), .Names = c("StartDate", "RelFreq"), row.names = c(NA,
5L), class = "data.frame")), .Names = c("word", "freq")), NAV = structure(list(
word = "NAV", freq = structure(list(StartDate = structure(1:5, .Label = c("2004-01-04 - 2004-01-10",
"2004-01-11 - 2004-01-17", "2004-01-18 - 2004-01-24", "2004-01-25 - 2004-01-31",
"2004-02-01 - 2004-02-07"), class = "factor"), RelFreq = c(67L,
55L, 62L, 79L, 60L)), .Names = c("StartDate", "RelFreq"), row.names = c(NA,
5L), class = "data.frame")), .Names = c("word", "freq"))), .Names = c("CAF",
"NAV"))
For ease of reading, the str output is here
> str(tst)
List of 2
$ CAF:List of 2
..$ word: chr "CAF"
..$ freq:'data.frame': 5 obs. of 2 variables:
.. ..$ StartDate: Factor w/ 5 levels "2004-01-04 - 2004-01-10",..: 1 2 3 4 5
.. ..$ RelFreq : int [1:5] 23 24 26 27 26
$ NAV:List of 2
..$ word: chr "NAV"
..$ freq:'data.frame': 5 obs. of 2 variables:
.. ..$ StartDate: Factor w/ 5 levels "2004-01-04 - 2004-01-10",..: 1 2 3 4 5
.. ..$ RelFreq : int [1:5] 67 55 62 79 60
I'd like to assign new values to all the StartDate elements nested inside the freq data frame across all list elements. Specifically here, I will be replacing all with the POSIXct date of the first date in the value. (i.e. 2004-01-04 above), though I'm looking for a general solution to apply to other variables in the list that is not reproduced here.
I have a function fun that can do the conversion given a StartDate vector as an input, but I couldn't figure out how to do a batch reassignment across the entire list.
At the moment I resorted to doing a for loop across the entire tst list. Is there a better way, preferrably vectorized?