*Editing in response to comments
I have a dataset that I am trying to prepare for analysis:
raw<-data.frame(
name=c("Place 1", "Place 2", "Place 3", "Place 4"),
x.1.Jan.12=c(1, NA, 0.5, NA),
Jan.time=c("0900", NA, "0930", NA),
x.15.Jan.12=c(NA, 0.7, NA, NA),
Jan.time=c(NA, "1030", NA, NA),
x.3.Feb.12=c(0.8, 0.6, 0.4, NA),
Feb.time=c("0715", "0800", "0830", NA),
x.8.Feb.12=c(NA, NA, 0.65, 0.33),
Feb.time=c(NA, NA, "?", "1123")
)
The data should be quite straightforward: a location with a result, a date for the result and the time it was collected. As you’ll see, date has been used to name the variable containing the result. Each ‘time’ variable relates to the column before it – the first ‘Jan.time’ variable is the time of the results in ‘x.1.Jan.12’
I want to restructure the data into four variables – name, date, time and value.
I’m pretty sure reshape2 can do it and have got the data melted:
mDat<-melt(raw, id=c("name"))
Can’t work out the next steps – probably to do with the wierd variable names.
The result I’d like is along these lines:
outData<-data.frame(
name=c("Place 1", "Place 2", "Place 3", "Place 4", "Place 1", "Place 2", "Place 3", "Place 4", "Place 1", "Place 2", "Place 3", "Place 4", "Place 1", "Place 2", "Place 3", "Place 4"),
date=c("1-Jan-12", "1-Jan-12", "1-Jan-12", "1-Jan-12", "15-Jan-12", "15-Jan-12", "15-Jan-12", "15-Jan-12", "3-Feb-12", "3-Feb-12", "3-Feb-12", "3-Feb-12", "8-Feb-12", "8-Feb-12", "8-Feb-12", "8-Feb-12"),
value=c(1, NA, 0.5, NA, NA, 0.7, NA, NA, 0.8, 0.6, 0.4, NA, NA, NA, 0.65, 0.33),
time=c("0900", NA, "0930", NA, NA, "1030", NA, NA, "0715", "0800", "0830", NA, NA, NA, "?", "1123")
)
One option is to use
melt()from “reshape2” on different subsets of yourdata.frame. The subsets can be extracted usinggrep().