When there are multiple variables in a data.frame that need to be melted, I'm confused about how to make that work. Here's an example:
Data <- data.frame(SampleID = rep(1:10, each = 3),
TimePoint = rep(LETTERS[1:3], 10))
Data$File.ESIpos <- paste("20141031 Subject", Data$SampleID, "Point",
Data$TimePoint, "ESIpos")
Data$Date.ESIpos <- "20141031"
Data$File.ESIneg <- paste("20141030 Subject", Data$SampleID, "Point",
Data$TimePoint, "ESIneg")
Data$Date.ESIneg <- "20141030"
Data$File.APCIpos <- paste("20141029 Subject", Data$SampleID, "Point",
Data$TimePoint, "APCIpos")
Data$Date.APCIpos <- "20141029"
I would like that to be melted by both Date and File so that the new data.frame has the columns "SampleID, "TimePoint", a new column "Mode" (where the choices are ESIpos, ESIneg, and APCIpos), "File", and "Date". Here's the closest I've gotten with the reshape() function.
Data.long <- reshape(Data,
varying = c("File.ESIpos", "Date.ESIpos",
"File.ESIneg", "Date.ESIneg",
"File.APCIpos", "Date.APCIpos"),
idvar = c("SampleID", "TimePoint"),
ids = c("ESIpos", "ESIneg", "APCIpos"),
v.names = c("Date", "File"),
sep = ".",
direction = "long")
The output is a data.frame with the columns "SampleID", "TimePoint", "time" (which is "1", "2", or "3" for "ESIpos", "ESIneg", or "APCIpos"), "Date" and "File".
The first problem is that I don't see how to define a new "Mode" column. I can change the column "time" to be named "Mode", of course, but isn't there some way to tell it that the levels should be "ESIpos", "ESIneg", and "APCIpos" rather than 1, 2, 3? I thought I was doing that with ids = c("ESIpos"...
, but clearly I'm not. Plus, I get the same output regardless of whether I include the ids = c("ESIpos"...
line.
A second smaller issue is that regardless of whether I say v.names = c("Date", "File")
or v.names = c("File", "Date")
, the columns are always swapped, i.e. I get file names in the Date column and vice versa.