gpt4 book ai didi

r - pivot_wider 在从长格式 reshape 为宽格式时引入 NA 值

转载 作者:行者123 更新时间:2023-12-05 09:07:49 25 4
gpt4 key购买 nike

<分区>

我在使用 pivot_wider() reshape 数据时遇到问题。

我的数据是这样的:

df <- data.frame(area = c( "Area1","Area1","Area1","Area2","Area2","Area2","Area3","Area3","Area3"),
species = c("species1","species2","species3","species1","species2","species3","species1","species2","species3"),
season= c("Season1","Season1","Season1","Season2","Season2","Season2","Season3","Season3","Season3"),
value= c(2,3,5,7,9,2,6,9,3))

我可以将数据框更改为宽格式,如下所示。

df_wide <- df %>%
mutate(row = row_number()) %>%
pivot_wider(id_cols= c(row,species),
,names_from = "season",
values_from = "value") %>%
select(-row)

这是输出图。

enter image description here

我的问题是它引入了 NA,因为 pivot_wider() 为每个值创建了一个新列。

enter image description here

如果你能帮助我,我会很棒......

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