I have some data that has been given to me and looks like this:
each line is an experiment conducted, controling 5 variables (the first five columns) and gave results (readings) stored in the following (hundreds of) columns X, X1...etc.
I would like to melt my data.frame so that I just have one result variable storing all the readings of all the experiments, keeping the 5 explanatory variables. The problem is that each experiment yielded a different number of readings. So i don't know how to do this.
I dput an extract of my data.frame below:
structure(list(dose = c(0, 0.65, 1.625, 3.25, 6.1), ponte = structure(c(1L,
1L, 1L, 1L, 1L), .Label = c("P252224", "P312256"), class = "factor"),
jour = structure(c(3L, 3L, 3L, 3L, 3L), .Label = c("J1",
"J2", "J3"), class = "factor"), normalise = structure(c(1L,
1L, 1L, 1L, 1L), .Label = "non", class = "factor"), box = structure(c(1L,
1L, 1L, 1L, 1L), .Label = c("FB1", "FB2"), class = "factor"),
X = c(4653.625, 3965.312, 7922.779, 6688.122, 9635.559),
X.1 = c(4020.349, 4630.506, 8388.648, 7496.008, NA), X.2 = c(NA,
4695.361, 6832.299, 8592.608, NA), X.3 = c(NA, NA, 7504.947,
9375.783, NA), X.4 = c(NA, NA, 8391.027, 9080.961, NA), X.5 = c(NA,
NA, NA, 10213.631, NA)), .Names = c("dose", "ponte", "jour",
"normalise", "box", "X", "X.1", "X.2", "X.3", "X.4", "X.5"
), row.names = c(NA, -5L), class = "data.frame")
This code removes all columns which names starts with
X
and addsX.All
column containing row averages of allX
columns. You didn't mention how you want to aggregate non-NA X.n values so replacemean
with your desired function: