I am encountering a problem when I reshape my data using pivot_wider()
.
My data looks like this:
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))
I am able to change the data frame to the wide format as below.
df_wide <- df %>%
mutate(row = row_number()) %>%
pivot_wider(id_cols= c(row,species),
,names_from = "season",
values_from = "value") %>%
select(-row)
this is the figure of output.
My problem is that it introduces NAs because pivot_wider()
makes a new column for each value.
If you help me, I would be great...