2

*根据评论进行编辑

我有一个数据集,我正在尝试为分析做准备:

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”中结果的时间

我想将数据重组为四个变量- namedate和。我很确定 reshape2 可以做到,并且已经将数据融化:timevalue

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")
)
4

2 回答 2

1

一种选择是melt()在你的不同子集上使用“reshape2” data.frame。可以使用 提取子集grep()

library(reshape2)
temp <- cbind(
    setNames(melt(raw[c(1, grep("time", names(raw)))], id.vars="name"), 
             c("name", "mon.time", "time")),
    setNames(melt(raw[grep("time", names(raw), invert = TRUE)], id.vars="name"),
             c("name", "date", "result")))
temp[, c("name", "result", "time", "date")]
#       name result time        date
# 1  Place 1   1.00 0900  x.1.Jan.12
# 2  Place 2     NA <NA>  x.1.Jan.12
# 3  Place 3   0.50 0930  x.1.Jan.12
# 4  Place 4     NA <NA>  x.1.Jan.12
# 5  Place 1     NA <NA> x.15.Jan.12
# 6  Place 2   0.70 1030 x.15.Jan.12
# 7  Place 3     NA <NA> x.15.Jan.12
# 8  Place 4     NA <NA> x.15.Jan.12
# 9  Place 1   0.80 0715  x.3.Feb.12
# 10 Place 2   0.60 0800  x.3.Feb.12
# 11 Place 3   0.40 0830  x.3.Feb.12
# 12 Place 4     NA <NA>  x.3.Feb.12
# 13 Place 1     NA <NA>  x.8.Feb.12
# 14 Place 2     NA <NA>  x.8.Feb.12
# 15 Place 3   0.65    ?  x.8.Feb.12
# 16 Place 4   0.33 1123  x.8.Feb.12
于 2013-01-11T11:17:45.567 回答
0

新的一天通常会有所帮助。我设法制定了一个非重塑解决方案,但它使用了一个可怕的 for 循环:

subList<-list()
for(i in seq(2,8,2)){
  temp<-raw[c(1, i, i+1)]
  temp$date<-rep(names(temp)[2], nrow(temp))
  names(temp)<-c("name", "result", "time", "date")
  subList[[i/2]]<-temp
}

solution1<-do.call("rbind", subList)
于 2013-01-11T09:25:54.720 回答