我一定遗漏了一些微不足道的东西,但我不明白为什么下面的演员将我的数据框转换为列表。如果可能的话,我希望输出是一个数据框。
它开始于
str(d)
'data.frame': 12 obs. of 4 variables:
$ credit_id: num 12 12 12 12 18 ...
$ Date : Date, format: "2003-06-30" "2003-09-30" "2003-12-31" ...
$ value : num 840 854 847 834 4831 ...
$ varb : chr "sales" "sales" "sales" "sales" ...
然后我尝试投射它
d<-cast(d,Date+credit_id~varb)
我得到
字符串(d)
List of 4
$ Date : Date[1:9], format: "2003-06-30" "2003-06-30" "2003-09-30" ...
$ credit_id: num [1:9] 12 18 12 18 12 ...
$ ebitda : num [1:9] NA NA NA NA NA ...
$ sales : num [1:9] 840 4831 854 4670 847 ...
- attr(*, "row.names")= int [1:9] 1 2 3 4 5 6 7 8 9
- attr(*, "idvars")= chr [1:2] "Date" "credit_id"
- attr(*, "rdimnames")=List of 2
..$ :'data.frame': 9 obs. of 2 variables:
.. ..$ Date : Date[1:9], format: "2003-06-30" "2003-06-30" "2003-09-30" ...
.. ..$ credit_id: num [1:9] 12 18 12 18 12 ...
..$ :'data.frame': 2 obs. of 1 variable:
.. ..$ varb: chr [1:2] "ebitda" "sales"
完整代码如下。提前致谢。
d<-structure(list(credit_id = c(12, 12, 12, 12, 18, 18, 2073, 2073,
2103, 2103, 1776, 1776), Date = structure(c(12233, 12325, 12417,
12508, 12233, 12325, 15552, 15552, 15552, 15552, 15552, 15552
), class = "Date"), value = c(839.8, 853.9, 846.9, 833.7, 4831.2,
4670, 54.1, 995, 90.944, 1092.8, 81.2, 1348.2), varb = c("sales",
"sales", "sales", "sales", "sales", "sales", "ebitda", "sales",
"ebitda", "sales", "ebitda", "sales")), .Names = c("credit_id",
"Date", "value", "varb"), row.names = c(606799L, 606800L, 606801L,
606802L, 606805L, 606806L, 1131814L, 1131822L, 1131950L, 1131958L,
1132034L, 1132042L), class = "data.frame")
head(d)
str(d)
d<-cast(d,Date+credit_id~varb)
head(d)
str(d)