gistlib
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 chars16 lines
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)
gistlibby LogSnag