Reputation: 391
I have a data frame
mat<-matrix(rnorm(100),20,5)
d<-data.frame(factor=as.factor(rep(c(1,2),each=10)),mat)
factor X1 X2 X3
1 1 -0.32946204 0.04911047 -1.4746314
2 1 -0.36187765 -0.36763007 0.2300900
3 1 0.77672015 1.38866125 -0.1773993
4 1 0.77633728 -1.14792986 1.2762236
5 1 0.41777137 -2.19985513 -0.5128805
6 1 -0.71586631 0.33818931 1.4241757
7 1 1.09668610 -1.33478667 0.7134252
8 1 0.03514851 -0.42269620 0.2069336
9 1 0.80135492 -0.53586509 -1.6818566
10 1 -0.60833750 1.27533884 -0.1066936
11 2 1.61718020 -0.34770915 0.6308279
12 2 4.15932386 -2.61849747 0.9735757
13 2 0.19107478 -2.32520215 -0.3536511
14 2 -0.52132006 -0.63421868 0.6935504
15 2 1.10620346 -1.40234676 1.2600821
16 2 -1.49278432 -0.09365126 0.8021205
17 2 0.31906474 -0.27821518 1.4890480
18 2 -0.94842397 -0.48807032 -0.8057988
19 2 -0.54172629 0.04712952 1.2591512
20 2 -0.43303452 0.06493442 -0.3316403
how do I convert the factors into just two observations as 1 and 2 and all the values added correspondingly
factor X1 X2 X3 X1 X2 X3 X1 X2 X3 ..........................
1
2
i want all the observations corresponding to 1 and 2 to come in one row. I don't want sum or mean. I just want all the observations in two columns for above exampl
Upvotes: 1
Views: 39
Reputation: 3017
You can do this with dplyr
and tidyr
.
library(dplyr)
library(tidyr)
mat <- matrix(rnorm(100),20,5)
d <- data.frame(factor=as.factor(rep(c(1,2),each=10)),mat) %>% as_data_frame()
d %>%
group_by(factor) %>%
mutate(n = row_number()) %>%
gather("key", "val", -factor, -n) %>%
unite(key_n, key, n) %>%
spread(key_n, val)
#> Source: local data frame [2 x 51]
#> Groups: factor [2]
#>
#> # A tibble: 2 x 51
#> factor X1_1 X1_10 X1_2 X1_3 X1_4 X1_5
#> * <fctr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 0.5595506 -0.9253696 -1.0211724 1.413117 -0.8939388 1.071695
#> 2 2 0.3953686 -0.6118039 -0.3091567 -1.076853 -1.4190182 2.607704
#> # ... with 44 more variables: X1_6 <dbl>, X1_7 <dbl>, X1_8 <dbl>,
#> # X1_9 <dbl>, X2_1 <dbl>, X2_10 <dbl>, X2_2 <dbl>, X2_3 <dbl>,
#> # X2_4 <dbl>, X2_5 <dbl>, X2_6 <dbl>, X2_7 <dbl>, X2_8 <dbl>,
#> # X2_9 <dbl>, X3_1 <dbl>, X3_10 <dbl>, X3_2 <dbl>, X3_3 <dbl>,
#> # X3_4 <dbl>, X3_5 <dbl>, X3_6 <dbl>, X3_7 <dbl>, X3_8 <dbl>,
#> # X3_9 <dbl>, X4_1 <dbl>, X4_10 <dbl>, X4_2 <dbl>, X4_3 <dbl>,
#> # X4_4 <dbl>, X4_5 <dbl>, X4_6 <dbl>, X4_7 <dbl>, X4_8 <dbl>,
#> # X4_9 <dbl>, X5_1 <dbl>, X5_10 <dbl>, X5_2 <dbl>, X5_3 <dbl>,
#> # X5_4 <dbl>, X5_5 <dbl>, X5_6 <dbl>, X5_7 <dbl>, X5_8 <dbl>, X5_9 <dbl>
Upvotes: 3
Reputation: 70653
You can split by factor
, reflow the data into wide format and combine the result.
d <- split(d, f = d$factor)
out <- sapply(d, FUN = function(x) {
x$seqi <- 1:nrow(x)
reshape(x, idvar = "factor", timevar = "seqi", direction = "wide")
}, simplify = FALSE)
do.call(rbind, out)
factor X1.1 X2.1 X3.1 X4.1 X5.1
1 1 0.4954632 2.1297804 -1.0001047 -0.04452902 0.07515041
2 2 0.6782848 -0.9476608 -0.3492558 0.41029783 0.78760862
X1.2 X2.2 X3.2 X4.2 X5.2 X1.3
1 -1.276254 -1.33387 2.153656 0.6856235 -0.6053279 0.4786269
2 1.185984 1.25427 -1.120756 0.8260916 -0.6193034 0.5087422
...
Upvotes: 2