强制转换将数据帧转换为r(重塑包)中的列表-希望保留为数据帧
我肯定遗漏了一些琐碎的东西,但我不明白为什么下面的cast会将我的数据帧转换为列表。如果可能的话,我希望输出是一个数据帧 一开始是强制转换将数据帧转换为r(重塑包)中的列表-希望保留为数据帧,r,list,casting,reshape,R,List,Casting,Reshape,我肯定遗漏了一些琐碎的东西,但我不明白为什么下面的cast会将我的数据帧转换为列表。如果可能的话,我希望输出是一个数据帧 一开始是 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
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<-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)
d使用重塑2软件包中的dcast
。从?dcast
可以了解到dcast
将数据帧作为输出
set.seed(001) # Generating some data.
credit_id <- sample(c(12,14,13,11), 10, TRUE)
Date <- seq(Sys.Date(), length.out=10, by="1 day")
value <- rnorm(10,1000,50)
varb <- sample(c("ebitda", "sales"), 10, TRUE)
d <- data.frame(credit_id, Date, value, varb) # this is something like your df
str(d)
'data.frame': 10 obs. of 4 variables:
$ credit_id: num 14 14 13 11 12 11 11 13 13 12
$ Date : Date, format: "2012-09-27" "2012-09-28" ...
$ value : num 959 1024 1037 1029 985 ...
$ varb : Factor w/ 2 levels "ebitda","sales": 1 2 1 1 2 2 2 1 2 1
d2 <- dcast(d,Date+credit_id~varb)
str(d2)
'data.frame': 10 obs. of 4 variables:
$ Date : Date, format: "2012-09-27" "2012-09-28" ...
$ credit_id: num 14 14 13 11 12 11 11 13 13 12
$ ebitda : num 959 NA 1037 1029 NA ...
$ sales : num NA 1024 NA NA 985 ...
下次请确保使用dput(数据框)
提供一些数据,以使您的问题具有可复制性。请使用Reformae2软件包中的dcast
。从?dcast
可以了解到dcast
将数据帧作为输出
set.seed(001) # Generating some data.
credit_id <- sample(c(12,14,13,11), 10, TRUE)
Date <- seq(Sys.Date(), length.out=10, by="1 day")
value <- rnorm(10,1000,50)
varb <- sample(c("ebitda", "sales"), 10, TRUE)
d <- data.frame(credit_id, Date, value, varb) # this is something like your df
str(d)
'data.frame': 10 obs. of 4 variables:
$ credit_id: num 14 14 13 11 12 11 11 13 13 12
$ Date : Date, format: "2012-09-27" "2012-09-28" ...
$ value : num 959 1024 1037 1029 985 ...
$ varb : Factor w/ 2 levels "ebitda","sales": 1 2 1 1 2 2 2 1 2 1
d2 <- dcast(d,Date+credit_id~varb)
str(d2)
'data.frame': 10 obs. of 4 variables:
$ Date : Date, format: "2012-09-27" "2012-09-28" ...
$ credit_id: num 14 14 13 11 12 11 11 13 13 12
$ ebitda : num 959 NA 1037 1029 NA ...
$ sales : num NA 1024 NA NA 985 ...
下次一定要使用dput(dataframe)
提供一些数据,以使你的问题重现。class(d)
显示它仍然是数据。frame
,它是列表的一种特殊形式。然而,它是第一类的cast_df
,它有自己的str
方法,所以它看起来不同。谢谢@James。也许我不应该那么害怕列表:)class(d)
揭示了它仍然是一个data.frame
,它是列表的一种特殊形式。然而,它是第一类的cast_df
,它有自己的str
方法,所以它看起来不同。谢谢@James。也许我不应该那么害怕列表:)谢谢@Jilber,很高兴知道dcast。很抱歉没有让事情重现。我使用dput并将结果复制到染料中,这是使用dput
的方法。我几乎忘了说重塑是重塑2的旧版本,请参见问题以获得一些解释。很高兴能帮上忙。谢谢@Jilber,很高兴了解dcast。很抱歉没有让事情重现。我使用dput并将结果复制到染料中,这是使用dput
的方法。我几乎忘了说重塑是重塑2的旧版本,请参见问题以获得一些解释。很高兴有用。
d
credit_id Date value varb
1 14 2012-09-27 958.9766 ebitda
2 14 2012-09-28 1024.3715 sales
3 13 2012-09-29 1036.9162 ebitda
4 11 2012-09-30 1028.7891 ebitda
5 12 2012-10-01 984.7306 sales
6 11 2012-10-02 1075.5891 sales
7 11 2012-10-03 1019.4922 sales
8 13 2012-10-04 968.9380 ebitda
9 13 2012-10-05 889.2650 sales
10 12 2012-10-06 1056.2465 ebitda
d2
Date credit_id ebitda sales
1 2012-09-27 14 958.9766 NA
2 2012-09-28 14 NA 1024.3715
3 2012-09-29 13 1036.9162 NA
4 2012-09-30 11 1028.7891 NA
5 2012-10-01 12 NA 984.7306
6 2012-10-02 11 NA 1075.5891
7 2012-10-03 11 NA 1019.4922
8 2012-10-04 13 968.9380 NA
9 2012-10-05 13 NA 889.2650
10 2012-10-06 12 1056.2465 NA