Vectors stack in R

I would like to stack several vectors so that I can perform repeated ANOVA measurements from the following data, but I'm not sure which command to use. The number of days must be laid.

The dataset is known as tbl.

Hydrocarbons    T0  2 days  5 days  8 days  12 days 16 days 21 days
C12-C16b        255 210 134 91  64  41  189
C17-C25 b       857 707 428 405 322 208 708
C26-C32 b       232 193 139 122 92  57  165
C33-C37 b             84    79  72  63  61  32  84
+3
source share
2 answers

I assume that you want to switch from wide format to long format, so using a command meltfrom the library reshape2will do the trick.

tbl.melt = melt(tbl, id.vars = "Hydrocarbons", variable.name = "measured.at", value.name = "measurement")

See more details ?melt.

I tried to make it a little more reproducible:

tbl.txt = "
Hydrocarbons    T0  2days   5days   8days   12days  16days  21days
C12-C16b        255 210 134 91  64  41  189
C17-C25b        857 707 428 405 322 208 708
C26-C32b        232 193 139 122 92  57  165
C33-C37b              84    79  72  63  61  32  84"

tbl = read.table(text=tbl.txt,header=TRUE)
tbl.melt = melt(tbl, id.vars="Hydrocarbons", variable.name = "measured.at", value.name = "measurement")
tbl.melt

Output:

Hydrocarbons measured.at value
1      C12-C16b          T0   255
2      C17-C25b          T0   857
3      C26-C32b          T0   232
4      C33-C37b          T0    84
5      C12-C16b      X2days   210
6      C17-C25b      X2days   707
7      C26-C32b      X2days   193
8      C33-C37b      X2days    79
9      C12-C16b      X5days   134
10     C17-C25b      X5days   428
11     C26-C32b      X5days   139
12     C33-C37b      X5days    72
13     C12-C16b      X8days    91
14     C17-C25b      X8days   405
15     C26-C32b      X8days   122
16     C33-C37b      X8days    63
17     C12-C16b     X12days    64
18     C17-C25b     X12days   322
19     C26-C32b     X12days    92
20     C33-C37b     X12days    61
21     C12-C16b     X16days    41
22     C17-C25b     X16days   208
23     C26-C32b     X16days    57
24     C33-C37b     X16days    32
25     C12-C16b     X21days   189
26     C17-C25b     X21days   708
27     C26-C32b     X21days   165
28     C33-C37b     X21days    84

Note that the “X” in timings is caused by reading from the text you provided and field names starting with a numeric value.

+1
source

, , R data.frame. , , data.frame:

data.frame( tbl[1], stack(tbl[-1]))

:

structure(list(Hydrocarbons = structure(1:4, .Label = c("C12-C16b", 
"C17-C25b", "C26-C32b", "C33-C37b"), class = "factor"), T0 = c(255L, 
857L, 232L, 84L), x2days = c(210L, 707L, 193L, 79L), x5days = c(134L, 
428L, 139L, 72L), x8days = c(91L, 405L, 122L, 63L), x12days = c(64L, 
322L, 92L, 61L), x16days = c(41L, 208L, 57L, 32L), x21days = c(189L, 
708L, 165L, 84L)), .Names = c("Hydrocarbons", "T0", "x2days", 
"x5days", "x8days", "x12days", "x16days", "x21days"), class = "data.frame", row.names = c(NA, 
-4L))

as.data.frame.table, , R, "" R. , :

 as.data.frame(tbl)
+2

All Articles