My data is in this form: var has 3 conditions (0,1,2)> dfvar 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]]
Thanks for the reproducible data set. The unstack() function produces a list of three vectors, one for each value of var, but it cannot combine them into a matrix since the number of entries in each is not the same. To get that you need to pad each vector with NAs:> 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")> datlst <- unstack(dat, cauc~var) > datlst$`0` [1] 1585.274 2481.678 8871.575 1013.527 1517.250 $`1` [1] 6462.3288 344.1781 $`2` [1] 816.7808 6031.3356 4913.5274> MaxL <- max(sapply(datlst, length)) > datmat <- sapply(datlst, function(x) c(x, rep(NA,MaxL-length(x))))> datmat0 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 ------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Wim Kreinen Sent: Monday, September 2, 2013 12:07 PM To: r-help Subject: [R] restructure my data My data is in this form: var has 3 conditions (0,1,2)> dfvar 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.
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.