2016-08-22 6 views
0

私は大きなデータフレームの形を変える方法を探していましたが、私はいくつかの困難を抱えています。私は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 = '.') 

答えて

1

あなたはあなたのデータを変換するためにgather機能を使用することができますtidyrパッケージを使用して、それを分割してベースRにバインドします。ggplot2で再生するので、最後にtableFinalを貼り付けますが、それぞれ独自の!

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

完全に機能しました。どうもありがとうございました。よろしく。 Matías。 –

関連する問題