0

I have this dataframe, where the first column gives time steps and every row gives me the values:

1  1.5  1.6  5.4  7.8
2  4.5  4.6  2.4  4.7
3  7.8  7.8  6.8  6.8 

I need to arrange them like this

1  1.5 
2  4.5  
3  7.8 
1  1.6
2  4.6
3  7.8
1  5.4
2  2.4
3  6.8
1  7.8
2  4.7
3  6.8

I tried simple gather() of tidyr but it doesn't do the trick cuz I also need to do the renumbering.

Any idea...I guess its just too simple

smci
  • 29,564
  • 18
  • 109
  • 144
Briis300
  • 11
  • 1
  • 6

2 Answers2

1

you can

data.frame(new1 = df[,1], new2 = unlist(df[,-1]))

#    new1 new2
#V21    1  1.5
#V22    2  4.5
#V23    3  7.8
#V31    1  1.6
#V32    2  4.6
#V33    3  7.8
#V41    1  5.4
#V42    2  2.4
#V43    3  6.8
#V51    1  7.8
#V52    2  4.7
#V53    3  6.8

For new1 you are using r e c y c l i n g.

Andre Elrico
  • 9,945
  • 4
  • 45
  • 65
0

With dplyr:

library(dplyr)

tt <- "id v1 v2 v3 v4
1  1.5  1.6  5.4 7.8
2  4.5  4.6  2.4  4.7
3  7.8  7.8  6.8  6.8" 

dat <- read.table(text = tt, header = T)

dat %>% 
  gather("id", "value") %>% 
  mutate(id = rep(1:3, 4))

#    id value
# 1   1   1.5
# 2   2   4.5
# 3   3   7.8
# 4   1   1.6
# 5   2   4.6
# 6   3   7.8
# 7   1   5.4
# 8   2   2.4
# 9   3   6.8
# 10  1   7.8
# 11  2   4.7
# 12  3   6.8
Andre Elrico
  • 9,945
  • 4
  • 45
  • 65
RLave
  • 7,816
  • 3
  • 20
  • 35