r - assigning values to nested data frames across elements in a list -


i have list called tst , reproducible 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, str output 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 assign new values startdate elements nested inside freq data frame across list elements. here, replacing posixct date of first date in value. (i.e. 2004-01-04 above), though i'm looking general solution apply other variables in list not reproduced here.

i have function fun can conversion given startdate vector input, couldn't figure out how batch reassignment across entire list.

at moment resorted doing for loop across entire tst list. there better way, preferrably vectorized?

if want retain listness of tst, then

tst2 <- lapply(tst,function(x) { x$freq$startdate <- as.posixct(x$freq$startdate); x; }); tst2; ## $caf ## $caf$word ## [1] "caf" ## ## $caf$freq ##    startdate relfreq ## 1 2004-01-04      23 ## 2 2004-01-11      24 ## 3 2004-01-18      26 ## 4 2004-01-25      27 ## 5 2004-02-01      26 ## ## ## $nav ## $nav$word ## [1] "nav" ## ## $nav$freq ##    startdate relfreq ## 1 2004-01-04      67 ## 2 2004-01-11      55 ## 3 2004-01-18      62 ## 4 2004-01-25      79 ## 5 2004-02-01      60 ## ## str(tst2); ## list of 2 ##  $ caf:list of 2 ##   ..$ word: chr "caf" ##   ..$ freq:'data.frame':  5 obs. of  2 variables: ##   .. ..$ startdate: posixct[1:5], format: "2004-01-04" "2004-01-11" "2004-01-18" "2004-01-25" ... ##   .. ..$ 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: posixct[1:5], format: "2004-01-04" "2004-01-11" "2004-01-18" "2004-01-25" ... ##   .. ..$ relfreq  : int [1:5] 67 55 62 79 60 

however, i'd make recommendation transform data data.frame, make lot of operations easier, including one:

df <- do.call(rbind,lapply(tst,function(x) cbind(word=x$word,x$freq))); df$startdate <- as.posixct(df$startdate); df; ##       word  startdate relfreq ## caf.1  caf 2004-01-04      23 ## caf.2  caf 2004-01-11      24 ## caf.3  caf 2004-01-18      26 ## caf.4  caf 2004-01-25      27 ## caf.5  caf 2004-02-01      26 ## nav.1  nav 2004-01-04      67 ## nav.2  nav 2004-01-11      55 ## nav.3  nav 2004-01-18      62 ## nav.4  nav 2004-01-25      79 ## nav.5  nav 2004-02-01      60 str(df); ## 'data.frame': 10 obs. of  3 variables: ##  $ word     : factor w/ 2 levels "caf","nav": 1 1 1 1 1 2 2 2 2 2 ##  $ startdate: posixct, format: "2004-01-04" "2004-01-11" "2004-01-18" "2004-01-25" ... ##  $ relfreq  : int  23 24 26 27 26 67 55 62 79 60 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -