为列表

时间:2015-05-31 02:01:14

标签: r list

我有一个名为tst的列表,可以使用下面的dput输出重现。

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"))

为便于阅读,str输出在这里

> 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

我想为所有列表元素中嵌套在StartDate数据框内的所有freq元素分配新值。具体来说,我将使用值中第一个日期的POSIXct日期替换所有日期。 (即上面的2004-01-04),虽然我正在寻找一个通用的解决方案来应用列表中其他未在此处复制的变量。

我有一个函数fun可以在StartDate向量作为输入的情况下进行转换,但我无法弄清楚如何在整个列表中进行批量重新分配。

目前我在整个for列表中进行tst循环。有没有更好的方法,最好是矢量化?

1 个答案:

答案 0 :(得分:1)

如果您想保留tst的清单,那么

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

但是,我还建议您将数据转换为data.frame,这将使大量操作变得更容易,包括以下内容:

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