我有一个看起来像这样的大数据框:
Location Dttm Parameter Unit Value
1 Airport 2018-01-01 12:00:00 Wind Direction deg 60
2 Airport 2018-01-01 12:00:00 Wind Speed m/sec 3.45
显然,这还有更多行,我只是在显示数据示例。我需要旋转数据,以便有一列用于风向和风速。但是,当我运行ivot_wider函数时,会得到如下信息:
Location Dttm Unit Wind Direction Wind Speed
1 Airport 2018-01-01 12:00:00 deg 60 NULL
2 Airport 2018-01-01 12:00:00 m/sec NULL 3.45
我尝试了各种group_by方法,但是没有找到任何能满足我实际需求的东西,
Location Dttm Wind Direction Wind Speed
1 Airport 2018-01-01 12:00:00 60 3.45
我认为,如果我将id_cols设置为Dttm,pivot_wider会为我完成此操作,但这也不起作用。甚至不确定如何真正用谷歌搜索该解决方案,因此我们将为您提供帮助!
答案 0 :(得分:4)
我们可以在执行Unit
之前删除pivot_wider
列
library(dplyr)
library(tidyr)
df1 %>%
select(-Unit) %>%
pivot_wider(names_from = Parameter, values_from = Value)
# A tibble: 1 x 4
# Location Dttm `Wind Direction` `Wind Speed`
# <chr> <dttm> <dbl> <dbl>
#1 Airport 2018-01-01 12:00:00 60 3.45
O在id_cols
中指定pivot_wider
参数(如@IceCreamToucan所示)
df1 %>%
pivot_wider(id_cols = -Unit, names_from = Parameter, values_from = Value)
如果该组有重复的行,请在pivot_wider
df1 %>%
group_by(Parameter) %>%
mutate(rn = row_number()) %>%
pivot_wider(id_cols = -Unit, names_from = Parameter, values_from = Value) %>%
select(-rn)
df1 <- structure(list(Location = c("Airport", "Airport"), Dttm = structure(c(1514826000,
1514826000), class = c("POSIXct", "POSIXt"), tzone = ""), Parameter = c("Wind Direction",
"Wind Speed"), Unit = c("deg", "m/sec"), Value = c(60, 3.45)), row.names = c("1",
"2"), class = "data.frame")
答案 1 :(得分:2)
使用from collections import Counter
counts = Counter(your_list)
可能会更好,但另一种可能性是使用如下所示的pivot_wider
。
tidyr::spread
输出
library(tidyr)
library(dplyr)
df1 <- structure(list(Location = c("Airport", "Airport"), Dttm = structure(c(1514826000,
1514826000), class = c("POSIXct", "POSIXt"), tzone = ""), Parameter = c("Wind Direction",
"Wind Speed"), Unit = c("deg", "m/sec"), Value = c(60, 3.45)), row.names = c("1",
"2"), class = "data.frame")
df1 %>%
select(-Unit) %>%
spread(Parameter, Value)