-2

How can I reshape this dataframe regarding the multiple information of the columns 2:7 (day_rep), obtaining this new "datlong" dataframe with the coumns: "trat" "day" "rep" "value"

dat <- read.table(header=T, text='
trat    5_1 5_2 6_1 6_2 7_1 7_2
control 0   0   0   0   12  66
control 0   0   0   0   14  76
control 0   0   0   0   18  78
control 0   0   0   0   16  74
control 0   0   0   0   20  76
urea    0   0   0   12  42  88
urea    0   0   0   8   34  76
urea    0   0   0   6   28  68
urea    0   0   0   4   40  60
urea    0   0   0   10  46  78
')
Juanchi
  • 1,147
  • 2
  • 18
  • 36

1 Answers1

2

We can use gather/separate from tidyr

library(dplyr)
library(tidyr)
gather(dat, dayrep, value, -trat) %>% 
        separate(dayrep, into=c("day", "rep")) %>%
        head()
#     trat day rep value
#1 control   5   1     0
#2 control   5   1     0
#3 control   5   1     0
#4 control   5   1     0
#5 control   5   1     0
#6    urea   5   1     0
akrun
  • 874,273
  • 37
  • 540
  • 662