我一直在寻找如何重塑大型数据帧,但我遇到了一些困难。我已经运行了srcipt,输出数据框是这样的(见下文):
这是脚本和带有示例数据库的链接。
full path
下面是我想要执行的数据框:
names(dataexample)
#To summary the categorical variables
str(dataexample)
# Transform
dataexample$Days<-as.numeric(as.character(dataexample$Days))
str(dataexample)
# Create a new column (polyname) combining treatment and block, separated by ","
dataexample$polyname <- paste(dataexample$Treatment, dataexample$Block, sep=",")
#Split the database and run approx function with the new column polyname
modelresult<-lapply(split(dataexample, dataexample$polyname), function(d) approx(d$Days, d$Variable, method="linear", xout=7:155, yleft=0, yright=0, rule = 1, f = 0, ties = mean ))
#Create a new table
Tableresult<-as.data.frame(modelresult)
This is the resulting table:
A.1.x A.1.y B.1.x B.1.y C.1.x C.1.y
7 0.00 7 0.00 7 0.00
8 0.02 8 0.02 8 0.02
9 0.04 9 0.04 9 0.04
10 0.06 10 0.06 10 0.06
. . . . . .
145 0.33 139 0.16 117 0.63
146 0.22 140 0.15 118 0.61
147 0.11 141 0.13 119 0.58
数据
A.1.x A.1.y 7 0.00
A.1.x A.1.y 8 0.02
A.1.x A.1.y 9 0.04
A.1.x A.1.y 10 0.06
A.1.x A.1.y . .
A.1.x A.1.y 145 0.33
A.1.x A.1.y 146 0.22
A.1.x A.1.y 147 0.11
B.1.x A.1.y 7 0.00
B.1.x B.1.y 8 0.02
B.1.x B.1.y 9 0.04
B.1.x B.1.y 10 0.06
B.1.x B.1.y . .
B.1.x B.1.y 139 0.16
B.1.x B.1.y 140 0.15
B.1.x B.1.y 141 0.13
C.1.x C.1.y 7 0.00
C.1.x C.1.y 8 0.02
C.1.x C.1.y 9 0.04
C.1.x C.1.y 10 0.06
C.1.x C.1.y . .
C.1.x C.1.y 117 0.63
C.1.x C.1.y 118 0.61
C.1.x C.1.y 119 0.58
答案 0 :(得分:1)
使用tidyr
包,您可以使用gather
函数转换数据,然后将其拆分并与基础R绑定。我最后会坚持使用tableFinal
使用ggplot2
可以更好地发挥,但对每个人都有更好的效果!
## install.packages('tidyr')
library('tidyr')
## gather the table
tableFinal <- tidyr::gather(Tableresult, Treatment, ModelValue)
## split the above table by x and y
tablex <- tableFinal[which(grepl('x', tableFinal$Treatment)), ]
colnames(tablex) <- c('TreatmentX', 'ModelValueX')
tabley <- tableFinal[which(grepl('y', tableFinal$Treatment)), ]
colnames(tabley) <- c('TreatmentY', 'ModelValueY')
## bind the two tables together
tableFinish <- cbind(tablex, tabley)