use pivot_wider to change two columns with values to wide format and keep others in long format in r in r

library(tidyr)

# Example dataframe
df <- data.frame(id = c(1, 1, 2, 2, 3, 3),
                 details = c("age", "gender", "age", "gender", "age", "gender"),
                 values = c(25, "M", 30, "F", 28, "M"))

# Using pivot_wider to spread 'details' and 'values' columns while keeping 'id' column in long format
wide_df <- df %>% pivot_wider(names_from = details, values_from = values)

# Print the resultant dataframe
print(wide_df)
441 chars
13 lines

related categories

gistlibby LogSnag