2017-01-17 3 views
4

Iは、各被験者のために海馬体積の複数の時点でデータセットを持っています。各海馬ボリュームには、左右の測定値があります。私は今、縦方向に左右の変化を比較したいと思っています。私は時間点のための私のデータを再構築する方法を知っているが、私はそれに「側」のレベルを追加する方法がわかりません。2つのレベルの縦方向データ:どのように再形成するか?

long <- reshape(mydata, direction="long", varying=list(c(2,4,6),c(3,5,7)),idvar="SID", timevar="time", v.names=c("HippLeft","HippRight"), times=c("time1","time2","time3")) 

私は左と右にあるのためのレベルを取得するために二回再構築適用する必要があります:

mydata <- data.frame(SID=sample(1:150,400, replace=TRUE), hippLeft_T1=sample(6000:8000,400,replace=TRUE), hippRight_T1=sample(6000:8000,400,replace=TRUE),hippLeft_T2=sample(6000:8000,400,replace=TRUE), hippRight_T2=sample(6000:8000,400,replace=TRUE),hippLeft_T3=sample(6000:8000,400,replace=TRUE), hippRight_T3=sample(6000:8000,400,replace=TRUE)) 

これは私が縦方向にそれを再構築する方法を、次に示します。だからここ

は私の再現性のあるデータセットでありますそこ?あるいは、これを行う別の方法がありますか?ありがとう!

**私は何を取得しようとしていますが、次のされています。これを行うには enter image description here

+0

あなたが望んでいるものの例を投稿することができ、あなたの出力データはどのように見えるでしょうか? –

+1

だから私は例を追加しました、ありがとう – user6121484

答えて

3

一つの方法はunitegather、およびtidyrからseparateの組み合わせを使用することです:

library(tidyr) 
long <- mydata %>% unite("times1", hippLeft_T1,hippRight_T1) %>% 
        unite("times2", hippLeft_T2,hippRight_T2) %>% 
        unite("times3", hippLeft_T3,hippRight_T3) %>% 
        gather("times","Hipp",times1:times3) %>% 
        separate(Hipp,c("Left","Right")) %>% 
        gather("Side","Hipp",Left:Right) 

ノート:

  1. 最初にuniteそれぞれの左右の列時間T1T2、およびT3と名前これらの列times1times2、そしてその後times3
  2. を、キー列timesと値の欄に名前を付けるgatherこれらの3つの列Hipp
  3. separateLeftRight
  4. Hippコラムキー列Sideと値の欄に名前を付けるgatherLeftRightHipp

実際より良い方法は、時間上の第1の合一で2 gather操作を逆にすることである:gatherにコールを1つだけ使用して

library(tidyr) 
long <- mydata %>% unite("Left", hippLeft_T1,hippLeft_T2,hippLeft_T3) %>% 
        unite("Right", hippRight_T1,hippRight_T2,hippRight_T3) %>% 
        gather("Side","Hipp",Left:Right) %>% 
        separate(Hipp,c("times1","times2","times3")) %>% 
        gather("times","Hipp",times1:times3) 

第三のアプローチがある:ここ

library(dplyr) 
library(tidyr) 
long <- mydata %>% gather("Side","Hipp",-SID) %>% 
        mutate(times=paste0("times",sub(".*(\\d)$","\\1",Side)), 
          Side=sub("^hipp([A-z]+)_T.*","\\1",Side)) %>% 
        select(SID,Side,times,Hipp) 

gatherからキー列Sideは、元mydataカラム名である値を持っています。私たちは、timesという名前のこの列の複製を作成するためにdeployer::mutateを使用しています。その後、我々はtimes値のために最後の桁を抽出し、Side値に対するLeftまたはRightのいずれかを抽出するために、いくつかの正規表現でsubを使用しています。

123にシードを設定すると、あなたのデータは次のとおりです。

set.seed(123) 
mydata <- data.frame(SID=sample(1:150,400, replace=TRUE), hippLeft_T1=sample(6000:8000,400,replace=TRUE), hippRight_T1=sample(6000:8000,400,replace=TRUE),hippLeft_T2=sample(6000:8000,400,replace=TRUE), hippRight_T2=sample(6000:8000,400,replace=TRUE),hippLeft_T3=sample(6000:8000,400,replace=TRUE), hippRight_T3=sample(6000:8000,400,replace=TRUE)) 
head(mydata) 
## SID hippLeft_T1 hippRight_T1 hippLeft_T2 hippRight_T2 hippLeft_T3 hippRight_T3 
##1 44  7973   6941  7718   7279  6319   7465 
##2 119  6274   6732  7775   6249  6289   7220 
##3 62  7811   6242  6978   6510  6298   6448 
##4 133  7153   6094  7436   7641  7029   7833 
##5 142  6791   6525  6973   7608  6986   7606 
##6 7  6900   7938  7978   6091  7233   6625 

第二または第三のアプローチのいずれかを使用して結果は次のとおりです。

print(long) 
##  SID Side times Hipp 
## 1 44 Left times1 7973 
## 2 119 Left times1 6274 
## 3 62 Left times1 7811 
## 4 133 Left times1 7153 
## 5 142 Left times1 6791 
## 6 7 Left times1 6900 
## ... 
## 401 44 Right times1 6941 
## 402 119 Right times1 6732 
## 403 62 Right times1 6242 
## 404 133 Right times1 6094 
## 405 142 Right times1 6525 
## 406 7 Right times1 7938 
## ... 
## 801 44 Left times2 7718 
## 802 119 Left times2 7775 
## 803 62 Left times2 6978 
## 804 133 Left times2 7436 
## 805 142 Left times2 6973 
## 806 7 Left times2 7978 
## ... 
##1201 44 Right times2 7279 
##1202 119 Right times2 6249 
##1203 62 Right times2 6510 
##1204 133 Right times2 7641 
##1205 142 Right times2 7608 
##1206 7 Right times2 6091 
## ... 
##1601 44 Left times3 6319 
##1602 119 Left times3 6289 
##1603 62 Left times3 6298 
##1604 133 Left times3 7029 
##1605 142 Left times3 6986 
##1606 7 Left times3 7233 
## ... 
##2001 44 Right times3 7465 
##2002 119 Right times3 7220 
##2003 62 Right times3 6448 
##2004 133 Right times3 7833 
##2005 142 Right times3 7606 
##2006 7 Right times3 6625 
+0

あなたの 'mydata'の' level'はどの列ですか? – aichao

+0

ありがとう!これはちょうど私に与える形がまさにそうですが、私はまた、時間(時間)だけでなく、横(左/右)のレベルを加えたいと思います。だから私は実際にネストされたデザインを持っています。それをどうやって得るのですか? – user6121484

+0

@ user6121484:編集を試してください。結果はあなたが望むように正確には順序付けられていませんが、長い形式はあなたが望むものです。 – aichao

関連する問題