[英]How can I reshape this data?
I have some data to reshape in R but can not figure out how. 我有一些要在R中重塑的数据,但不知道如何处理。 Here is the scenario: I have data like this 这是场景:我有这样的数据
a<- c("exam1", "exam2", "exam3","exam4")
date1<- c(8.2,4.3,6.7,3.9)
date2<- c(11.2,9.3,6.5,4.1)
date3<- c(8.2,9.1,4.3,4.4)
dr.df.a <- cbind(a,date1,date2,date3)
a date1 date2 date3
[1,] "exam1" "8.2" "11.2" "8.2"
[2,] "exam2" "4.3" "9.3" "9.1"
[3,] "exam3" "6.7" "6.5" "4.3"
[4,] "exam4" "3.9" "4.1" "4.4"
b<- c("exam1", "exam2", "exam3","exam4")
date1<- c(8.6,14.3,6.7,13.9)
date2<- c(11.2,8.3,16.5,14.1)
date3<- c(4.2,9.1,4.3,14.4)
dr.df.b <- cbind(b,date1,date2,date3)
b date1 date2 date3
[1,] "exam1" "8.6" "11.2" "4.2"
[2,] "exam2" "14.3" "8.3" "9.1"
[3,] "exam3" "6.7" "16.5" "4.3"
[4,] "exam4" "13.9" "14.1" "14.4"
mylist<–list(dr.df.a,dr.df.b)
The example is for reproducibly proposes. 该示例是可重复提出的。 I get the data in this format (dr.df.a and dr.df.b) There are multiple data frames in list object. 我以这种格式(dr.df.a和dr.df.b)获得数据。列表对象中有多个数据框。
Now I need to reshape it a way to get one single line and variable names like exam1_date1, exam1_date2 , exam1_date3, exam2_date1,exam2_date2 ...
and so on and essentially I would like to get data frame with rows of exam1_date1, exam1_date2 , exam1_date3, exam2_date1,exam2_date2 ...
for every data frame in list object. 现在,我需要重塑它的一种方式来获取单行和变量名称,例如, exam1_date1, exam1_date2 , exam1_date3, exam2_date1,exam2_date2 ...
等等,从本质exam1_date1, exam1_date2 , exam1_date3, exam2_date1,exam2_date2 ...
我想获取包含exam1_date1, exam1_date2 , exam1_date3, exam2_date1,exam2_date2 ...
对于列表对象中的每个数据帧, exam1_date1, exam1_date2 , exam1_date3, exam2_date1,exam2_date2 ...
How I can reshape this data and which function should I use ? 如何重塑这些数据以及应使用哪个功能?
Try this: 尝试这个:
library(reshape2)
# convert the first row (the one defined by variable 'a' in post) into column names
dr.df.2 <- setNames(dr.df[-1,], dr.df[1, ])
m <- melt(dr.df.2)
d <- dcast(m, 1 ~ ...)[-1]
names(d) <- sub("_", "_exam", names(d)) # fix up names (optional)
Giving this: 给这个:
> d
date1_exam1 date1_exam2 date1_exam3 date1_exam4 date2_exam1 date2_exam2
1 8.2 4.3 6.7 3.9 11.2 9.3
date2_exam3 date2_exam4 date3_exam1 date3_exam2 date3_exam3 date3_exam4
1 6.5 4.1 8.2 9.1 4.3 4.4
UPDATE: simplified dcast
formula 更新:简化的dcast
公式
If your dr.df
object were a data.frame
instead of a matrix, you can easily create a named vector as demonstrated below: 如果您的dr.df
对象是data.frame
而不是矩阵,则可以轻松创建命名向量,如下所示:
data.frame
this time: 创建数据,但是这次作为data.frame
: a <- c("exam1", "exam2", "exam3","exam4")
date1 <- c(8.2,4.3,6.7,3.9)
date2 <- c(11.2,9.3,6.5,4.1)
date3 <- c(8.2,9.1,4.3,4.4)
dr.df <- rbind(date1, date2, date3)
colnames(dr.df) <- a
dr.df <- as.data.frame(dr.df)
dr.df
# exam1 exam2 exam3 exam4
# date1 8.2 4.3 6.7 3.9
# date2 11.2 9.3 6.5 4.1
# date3 8.2 9.1 4.3 4.4
You can now simply use stack
to get the data in a long form. 现在,您可以简单地使用stack
来获取长格式的数据。
dr.dfL <- data.frame(stack(dr.df), date = rownames(dr.df))
The values for the vector you want are in the "values" column, and the names for those values can be obtained using paste
. 所需向量的值位于“值”列中,可以使用paste
获得这些值的名称。
setNames(dr.dfL$values, paste(dr.dfL$ind, dr.dfL$date, sep = "_"))
# exam1_date1 exam1_date2 exam1_date3 exam2_date1 exam2_date2 exam2_date3
# 8.2 11.2 8.2 4.3 9.3 9.1
# exam3_date1 exam3_date2 exam3_date3 exam4_date1 exam4_date2 exam4_date3
# 6.7 6.5 4.3 3.9 4.1 4.4
Note that the result here is just a named vector, not a data.frame
, as in the other answers. 请注意,这里的结果只是一个命名的向量,而不是data.frame
,就像其他答案一样。
You can use reshape from base R: 您可以从基数R使用重塑:
new <- reshape(dr, varying = list(c("date1","date2","date3")), direction = "long")
new$newname <- apply(new, 1, function(x) paste(x[1],paste("date",x[2],sep=""),sep="_"))
new <- new[,c("date1","newname")]
names(new) <- c("info","exam")
Outputs: 输出:
> new
info exam
1.1 8.2 exam1_date1
2.1 4.3 exam2_date1
3.1 6.7 exam3_date1
4.1 3.9 exam4_date1
1.2 11.2 exam1_date2
2.2 9.3 exam2_date2
3.2 6.5 exam3_date2
4.2 4.1 exam4_date2
1.3 8.2 exam1_date3
2.3 9.1 exam2_date3
3.3 4.3 exam3_date3
4.3 4.4 exam4_date3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.