繁体   English   中英

如何重塑数据框?

[英]How to reshape dataframe?

我一直在寻找如何重塑大型数据框,但遇到一些困难。 我已经运行了srcipt,并且输出数据帧是这样的(见下文):

这是脚本和示例数据库的链接。

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

数据

Tableresult <- read.table(header = TRUE, text = "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", na.strings = '.')

使用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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM