目前尚不清楚你想要什麼,但這裏的一些代碼aggregate
,讓你開始:
> df$values <- as.character(df$values)
> # A `list` of the values
> (da1 <- aggregate(values ~ date, df, I, simplify=FALSE))
date values
1 2000 a, b, d
> str(da1)
'data.frame': 1 obs. of 2 variables:
$ date : Factor w/ 1 level "2000": 1
$ values:List of 1
..$ 0:Class 'AsIs' chr [1:3] "a" "b" "d"
> # All the values collapsed into one string
> (da2 <- aggregate(values ~ date, df, paste, collapse = ", ", simplify=FALSE))
date values
1 2000 a, b, d
> str(da2)
'data.frame': 1 obs. of 2 variables:
$ date : Factor w/ 1 level "2000": 1
$ values:List of 1
..$ 0: chr "a, b, d"
我已經顯示了str
,因此您可以在這裏看到兩個示例之間的區別。
如果我下面的正確理解您的評論,你可能也有興趣在此:
> date = "2000"
> values = c("a", "b", "d")
> (temp <- data.frame(date, values = I(list(values))))
date values
1 2000 a, b, d
> str(temp)
'data.frame': 1 obs. of 2 variables:
$ date : Factor w/ 1 level "2000": 1
$ values:List of 1
..$ : chr "a" "b" "d"
..- attr(*, "class")= chr "AsIs"
換句話說,如果你想創建一個data.frame
你什麼時候有一個list
作爲列項必須使用I
函數。
請參閱'聚合'。 – A5C1D2H2I1M1N2O1R2T1 2013-04-25 09:28:16
如果你告訴我們你打算對結果數據做什麼,也許會更好。 – 2013-04-25 09:30:19
如果你想'values'作爲'list'然後做:'aggregate(values〜date,df,list)' – Arun 2013-04-25 09:32:30