Pivot wider with character values

Hello I want to do pivot wider to have a column "day" and a column "night" with all the diffente species in each. It's to do a diversity test between the day and the night

library(tidyverse)

df <- data.frame(
    species=c("car","bike","horse","boat","yacht","train"),
    station=c(A1,A1,A2,A2,A2,A3,),
   period = c(day,day,night,day,night,day)
    

that may help.

ps: please execute your examples data's code before pasting it :slight_smile:

library(tidyverse)

df <- data.frame(
  species=c("car","bike","horse","boat","yacht","train"),
  station=c("A1","A1","A2","A2","A2","A3"),
  period = c("day","day","night","day","night","day")
)
  

  
df %>% 
  group_by(station, period) %>% 
  summarise(n_dist_species = n_distinct(species)) %>% 
  pivot_wider(names_from = period, values_from = n_dist_species)


# # A tibble: 3 x 3
# # Groups:   station [3]
# station   day night
# <fct>   <int> <int>
# 1 A1          2    NA
# 2 A2          1     2
# 3 A3          1    NA

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.