convert a dataset into wide format but keep the names_from column as numeric in r

library(tidyr)
library(dplyr)

# Sample data
df <- data.frame(id = c(1, 2, 1, 2),
                 variable = c("A", "A", "B", "B"),
                 value = c(10, 20, 30, 40))

# Convert dataset into wide format
df_wide <- df %>%
  mutate(names_from = as.numeric(names_from)) %>%
  spread(key = variable, value = value)

# Output
print(df_wide)
346 chars
16 lines

related categories

gistlibby LogSnag