【发布时间】:2019-10-30 11:41:15
【问题描述】:
我有以下数据框:
df <- structure(list(locality_id = structure(c(3L, 1L, 2L, 4L), .Label = c("L2762894",
"L3064193", "L6199859", "L8044094"), class = "factor"), month = structure(c(1L,
3L, 1L, 2L), .Label = c("1", "2", "3"), class = "factor"), prec01 = c(105,
109, 133, 79), prec02 = c(29, 34, 34, 35), prec03 = c(18, 48,
42, 184)), class = "data.frame", row.names = c(NA, -4L))
每个locality 都有第一个three months (prec01, prec02, prec03) 的precipitation 值。
我想创建一个唯一的降水列,将每个月的对应降水值匹配到对应的月份(列月)。结果如下:
# locality_id month prec
# 1 L6199859 1 105
# 2 L2762894 3 109
# 3 L3064193 1 133
# 4 L8044094 2 184
知道如何产生上述输出吗?
谢谢!
【问题讨论】:
-
如果您可以使用循环(如果速度不是问题),那么这看起来很简单。
-
要获得比循环更好的解决方案,请查看以下内容:stackoverflow.com/questions/33542325/…(以及链接的重复项)。