[R] restructure my data

arun smartpink111 at yahoo.com
Mon Sep 2 23:51:15 CEST 2013


Hi,
You could try:
df2<- do.call(cbind,split(df[,-1],df[,1]))

 res<-sapply(seq_len(ncol(df2)),function(i) {x<-df2[,i];x[duplicated(x)]<-NA;x})
dimnames(res)<- dimnames(df2)
res
#            0         1         2
#[1,] 1585.274 6462.3288  816.7808
#[2,] 2481.678  344.1781 6031.3356
#[3,] 8871.575        NA 4913.5274
#[4,] 1013.527        NA        NA
#[5,] 1517.250        NA        NA
A.K.




----- Original Message -----
From: Wim Kreinen <wkreinen at gmail.com>
To: r-help <r-help at r-project.org>
Cc: 
Sent: Monday, September 2, 2013 1:07 PM
Subject: [R] restructure my data

My data is in this form: var has 3 conditions (0,1,2)

> df
   var      cauc
1    1 6462.3288
2    0 1585.2740
3    0 2481.6781
4    1  344.1781
5    0 8871.5753
6    2  816.7808
7    2 6031.3356
8    0 1013.5274
9    2 4913.5274
10   0 1517.2500

For the three conditions (0,1,2) I want the cauc-values to be listed like
this

0                         1               2
1585,2740       6462,3288     816.7808
2481.6781      344.1781       6031.3356
...

Thanks Wim

> dput (df)
structure(list(var = c(1, 0, 0, 1, 0, 2, 2, 0, 2, 0), cauc =
c(6462.32876712329,
1585.27397260274, 2481.67808219178, 344.178082191781, 8871.57534246575,
816.780821917808, 6031.33561643836, 1013.52739726027, 4913.52739726027,
1517.25)), .Names = c("var", "cauc"), row.names = c(NA, 10L), class =
"data.frame")
>

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list