Skip to content
Prev 383152 / 398502 Next

a simple reshape

Hi,

[For a non-tidyverse solution:]

Your problem is ambiguous without a 'time' variable; e.g., why should
the answer not be

test2  <- data.frame(vntr1=c("v1","v2"),
                     a1 =c(NA, 0.5693),
                     a2 = c(0.02, 0.12),
                     a3 =c(NA, 0.11),
                     a4=c(0.98, 0.04))

? If you do add an artificial time variable, say using

test1 <- transform(test1,
    time = unsplit(lapply(split(vntr1, vntr1), seq_along), vntr1))

to give
vntr1  val time
1    v1 0.98    1
2    v1 0.02    2
3    v2 0.59    1
4    v2 0.12    2
5    v2 0.11    3
6    v2 0.04    4

then either reshape() or dcast() easily gives you what you want:
vntr1 val.1 val.2 val.3 val.4
1    v1  0.98  0.02    NA    NA
3    v2  0.59  0.12  0.11  0.04
vntr1    1    2    3    4
1    v1 0.98 0.02   NA   NA
2    v2 0.59 0.12 0.11 0.04

-Deepayan
On Sat, Apr 4, 2020 at 12:28 AM Yuan Chun Ding <ycding at coh.org> wrote: