简体   繁体   中英

How to reshape dataframe?

I have been searching how to reshape a large dataframe but I am into some difficulties. I have run the srcipt and the output dataframe is like this (see below):

Here is the script and the link with an example database.

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

And below there is the dataframe that I would like to perform:

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

Data

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 = '.')

Using the tidyr package you can use the gather function to transform you data, then split it and bind it with base R. I would stick with tableFinal in the end as it plays better with ggplot2 , but to each their own!

## 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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