数据框行重新排列

时间:2014-04-05 09:40:21

标签: r

我有一个具有这种结构的数据框:

   str(locatie)
'data.frame':   465 obs. of  18 variables:
 $ year: int  1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 ...
 $ day  : int  1 2 3 4 5 6 7 8 9 10 ...
 $ q01 : num  0.411 0.368 0.368 0.368 0.368 0.368 0.326 0.326 0.326 0.326 ...
 $ q02 : num  0.368 0.368 0.368 0.368 0.368 0.368 0.368 0.411 0.411 0.499 ...
 $ q03 : num  0.518 0.558 0.558 0.558 0.558 0.518 0.518 0.478 2.6 4.35 ...
 $ q04 : num  0.518 0.478 0.438 0.478 0.518 0.478 2.16 3.7 2.74 0.758 ...
 $ q05 : num  0.149 0.162 0.149 0.149 0.149 0.149 0.149 0.149 0.149 0.149 ...
 $ q06 : num  1.77 0.44 0.359 0.336 0.302 0.286 0.286 0.3 0.309 0.333 ...
 $ q07 : num  0.461 0.407 0.413 0.391 0.424 0.663 1.42 1.72 2.11 1.82 ...
 $ q08 : num  0.24 0.217 0.265 0.422 0.445 0.424 0.336 0.353 0.344 0.319 ...
 $ q09 : num  0.593 0.7 0.667 0.657 0.657 0.632 0.657 0.745 0.751 0.707 ...
 $ q10 : num  2.85 2.48 2.33 2.26 2.4 3.1 3.94 4.52 4.68 4.68 ...
 $ q11 : num  2.07 1.54 0.518 0.518 0.438 0.478 0.478 0.478 0.478 0.398 ...
 $ q12 : num  0.282 0.287 0.359 0.316 0.316 0.292 0.316 0.292 0.267 0.222 ...

$ q*是一年中的几个月。

$ day是这几个月的日子。

我想根据年/日/变量/值重新排列它,如下所示:

year day variable value
1998  1  q01      0.411
1998  2  q01      0.368
.......................
1998  31 q01      0.368
1998  1  q02      0.368
1998  2  q02      0.368
.......................
1998  28 q02      0.518
1998  1  q03      0.518

到目前为止,我已尝试使用

重新排列它

test <- melt(locatie, id=(c("year", "day"))

但我对结果不满意,因为$ q01(1月)变量以这种形式传递到下一年(1999年):

   year day variable value
1  1998  1      q01 0.411
2  1998  2      q01 0.368
3  1998  3      q01 0.368
4  1998  4      q01 0.368
5  1998  5      q01 0.368
6  1998  6      q01 0.368
7  1998  7      q01 0.326
.........................
31 1998 31      q01 0.368
32 1999  1      q01 1.750
33 1999  2      q01 1.810

我有一长串数据,我不想手动重新排列。有什么建议?感谢。

稍后编辑:dput(head(locatie),32)

structure(list(year = c(1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1999L), day = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L), q01 = c(0.411, 0.368, 0.368, 0.368, 0.368, 0.368, 0.326, 0.326, 0.326, 0.326, 0.326, 0.326, 0.368, 0.411, 0.368, 0.368, 0.326, 0.326, 0.326, 0.368, 0.368, 0.368, 0.368, 0.368, 0.368, 0.368, 0.326, 0.368, 0.368, 0.368, 0.368, 1.75), q02 = c(0.368, 0.368, 0.368, 0.368, 0.368, 0.368, 0.368, 0.411, 0.411, 0.499, 0.411, 0.454, 0.454, 0.454, 2.82, 7.11, 7.2, 7.01, 2.8, 0.558, 0.598, 0.598, 0.678, 0.678, 0.598, 0.598, 0.558, 0.518, NA, NA, NA, 1.3), q03 = c(0.518, 0.558, 0.558, 0.558, 0.558, 0.518, 0.518, 0.478, 2.6, 4.35, 4.35, 4.35, 2.01, 0.598, 0.558, 0.558, 2.93, 4.18, 4.18, 4.18, 4.18, 4.02, 3.7, 2.19, 0.558, 1.36, 3, 1.99, 0.518, 0.518, 0.518, 4.84), q04 = c(0.518, 0.478, 0.438, 0.478, 0.518, 0.478, 2.16, 3.7, 2.74, 0.758, 0.598, 0.518, 0.518, 0.638, 0.518, 0.478, 0.478, 0.518, 0.598, 0.638, 0.518, 2.64, 4.36, 5.1, 1.23, 0.182, 0.158, 0.153, 0.149, 0.149, NA, 0.558), q05 = c(0.149, 0.162, 0.149, 0.149, 0.149, 0.149, 0.149, 0.149, 0.149, 0.149, 0.171, 0.23, 0.235, 0.227, 0.235, 0.227, 0.238, 0.782, 2.87, 3.09, 3.13, 3.07, 2.28, 0.636, 0.336, 0.258, 0.277, 0.277, 0.862, 1.92, 2.59, 3.15), q06 = c(1.77, 0.44, 0.359, 0.336, 0.302, 0.286, 0.286, 0.3, 0.309, 0.333, 0.44, 0.452, 0.424, 0.5, 0.558, 0.528, 1.78, 3.13, 2.71, 2.79, 2.94, 2.98, 2.87, 2.8, 2.69, 2.64, 1.93, 0.434, 0.434, 0.461, NA, 0.68), q07 = c(0.461, 0.407, 0.413, 0.391, 0.424, 0.663, 1.42, 1.72, 2.11, 1.82, 1.71, 1.6, 1.08, 0.788, 0.721, 0.928, 1.01, 1.35, 1.93, 1.93, 1.99, 1.48, 0.606, 0.239, 0.258, 0.222, 0.203, 0.173, 0.173, 0.244, 0.244, 3.28), q08 = c(0.24, 0.217, 0.265, 0.422, 0.445, 0.424, 0.336, 0.353, 0.344, 0.319, 0.28, 0.302, 0.28, 0.272, 0.272, 0.264, 0.363, 0.363, 0.363, 0.363, 0.363, 0.363, 0.376, 0.376, 0.376, 0.363, 0.426, 0.49, 0.514, 0.556, 0.568, 0.583), q09 = c(0.593, 0.7, 0.667, 0.657, 0.657, 0.632, 0.657, 0.745, 0.751, 0.707, 0.744, 0.809, 1, 1.34, 1.29, 1.29, 1.29, 1.39, 1.86, 2.19, 2.33, 2.33, 2.33, 2.77, 3.15, 3.23, 3.08, 3, 3.08, 3.46, NA, 0.478), q10 = c(2.85, 2.48, 2.33, 2.26, 2.4, 3.1, 3.94, 4.52, 4.68, 4.68, 4.68, 4.94, 5.36, 5.86, 5.36, 5.27, 5.36, 5.44, 5.1, 4.85, 3.16, 1.59, 1.81, 1.7, 2.06, 2.48, 1.81, 1.7, 1.99, 2.88, 2.44, 0.678), q11 = c(2.07, 1.54, 0.518, 0.518, 0.438, 0.478, 0.478, 0.478, 0.478, 0.398, 0.398, 0.398, 0.438, 0.638, 0.438, 0.438, 0.438, 0.398, 0.398, 0.263, 0.287, 0.287, 0.306, 0.306, 0.306, 0.3, 0.254, 0.277, 0.277, 0.3, NA, 0.478), q12 = c(0.282, 0.287, 0.359, 0.316, 0.316, 0.292, 0.316, 0.292, 0.267, 0.222, 0.222, 0.255, 0.271, 0.307, 0.271, 0.263, 0.285, 0.306, 0.28, 0.255, 0.255, 0.259, 0.289, 0.298, 0.344, 0.472, 0.816, 1.69, 1.81, 1.92, 1.92, 0.478)), .Names = c("year", "day", "q01", "q02", "q03", "q04", "q05", "q06", "q07", "q08", "q09", "q10", "q11", "q12"), row.names = c(NA, 32L), class = "data.frame")

1 个答案:

答案 0 :(得分:3)

如果我理解正确,主要问题是在melt编辑后以所需方式排序数据。

# a small subset of locatie, with two 'q columns'
df <- locatie[ , c("year", "day", "q01", "q02")]
df
#    year day   q01   q02
# 1  1998   1 0.411 0.368
# 2  1998   2 0.368 0.368
# ...snip
# 30 1998  30 0.368    NA
# 31 1998  31 0.368    NA
# 32 1999   1 1.750 1.300    

# melt
df2 <- melt(data = df, id.var = c("year", "day"))
df2
#    year day variable value
# 1  1998   1      q01 0.411
# 2  1998   2      q01 0.368
# ...snip
# 30 1998  30      q01 0.368
# 31 1998  31      q01 0.368
# 32 1999   1      q01 1.750 # A 1999 among the 1998s!
# 33 1998   1      q02 0.368
# 34 1998   2      q02 0.368
# ...snip
# 62 1998  30      q02    NA
# 63 1998  31      q02    NA
# 64 1999   1      q02 1.300

melt数据之后,它不是所需的顺序。现在,按年份变量,变量,而不是:

df2[order(df2$year, df2$variable, df2$day), ]

#    year day variable value
# 1  1998   1      q01 0.411
# 2  1998   2      q01 0.368
# ...snip
# 30 1998  30      q01 0.368
# 31 1998  31      q01 0.368
# 33 1998   1      q02 0.368
# 34 1998   2      q02 0.368
# ...snip
# 62 1998  30      q02    NA
# 63 1998  31      q02    NA
# 32 1999   1      q01 1.750
# 64 1999   1      q02 1.300