*根据评论进行编辑
我有一个数据集,我正在尝试为分析做准备:
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")
)
数据应该非常简单:带有结果的位置、结果的日期和收集时间。如您所见,日期已用于命名包含结果的变量。每个“时间”变量都与它之前的列相关 - 第一个“Jan.time”变量是“x.1.Jan.12”中结果的时间
我想将数据重组为四个变量- name
、date
和。我很确定 reshape2 可以做到,并且已经将数据融化:time
value
mDat<-melt(raw, id=c("name"))
无法完成接下来的步骤 - 可能与奇怪的变量名有关。
我想要的结果是这样的:
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")
)