gpt4 book ai didi

r - dcast 特定列并保留所有

转载 作者:行者123 更新时间:2023-12-04 05:47:38 24 4
gpt4 key购买 nike

我正在尝试为每种类型的 gridNumber 的每种类型的元素取一个具有特定值的列。和 dcast它以便从元素列创建 3 个单独的列。我不确定如何做到这一点。

输入:

df <- structure(list(date = structure(c(-25584, -25584, -25584, -25583, 
-25583, -25583, -25582, -25582, -25582, -25581), class = "Date"),
year = c(1899, 1899, 1899, 1899, 1899, 1899, 1899, 1899,
1899, 1899), month = c(12, 12, 12, 12, 12, 12, 12, 12, 12,
12), day = c(15, 15, 15, 16, 16, 16, 17, 17, 17, 18), gridNumber = c(526228,
526228, 526228, 526228, 526228, 526228, 526229, 526229, 526229,
526229), element = c("PPT", "TMAX", "TMIN", "PPT", "TMAX",
"TMIN", "PPT", "TMAX", "TMIN", "PPT"), value = c(0, 43.4782,
21.7403, 0, 43.3297, 20.751, 0, 57.3625, 25.8157, 0.2105)), .Names = c("date",
"year", "month", "day", "gridNumber", "element", "value"), row.names = c(NA,
10L), class = "data.frame")

数据框:
         date year month day gridNumber element   value
1 1899-12-15 1899 12 15 526228 PPT 0.0000
2 1899-12-15 1899 12 15 526228 TMAX 43.4782
3 1899-12-15 1899 12 15 526228 TMIN 21.7403
4 1899-12-16 1899 12 16 526228 PPT 0.0000
5 1899-12-16 1899 12 16 526228 TMAX 43.3297
6 1899-12-16 1899 12 16 526228 TMIN 20.7510
7 1899-12-17 1899 12 17 526229 PPT 0.0000
8 1899-12-17 1899 12 17 526229 TMAX 57.3625
9 1899-12-17 1899 12 17 526229 TMIN 25.8157
10 1899-12-18 1899 12 18 526229 PPT 0.2105

dcast 尝试:
newdat <- dcast(df, date ~ element)

所需的输出列:
date year month day gridNumber PPT TMAX TMIN value

最佳答案

这可能不是您想要的,因为您有一个单独的值列。那么,PPT、TMAX、TMIN 下放的是什么?

以下是如何使用 dplyr 将值放在适当的列下和 tidyr :

library(dplyr)
library(tidyr)
df1 %>%
spread(element,value)
date year month day gridNumber PPT TMAX TMIN
1 1899-12-15 1899 12 15 526228 0.0000 43.4782 21.7403
2 1899-12-16 1899 12 16 526228 0.0000 43.3297 20.7510
3 1899-12-17 1899 12 17 526229 0.0000 57.3625 25.8157
4 1899-12-18 1899 12 18 526229 0.2105 NA NA

可以使用 tidyr 写成一行只要:
spread(df1,element,value)

关于r - dcast 特定列并保留所有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34234452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com