Quantcast
Channel: Convert a columns into separate columns in wide format in R - Stack Overflow
Browsing all 6 articles
Browse latest View live

Answer by TarJae for Convert a columns into separate columns in wide format in R

Here are two more:library(dplyr)df %>% cbind(model.matrix(~ FRUITS + 0, .) == 1)library(nnet)df %>% cbind(class.ind(.$FRUITS) == 1)

View Article


Answer by GuedesBF for Convert a columns into separate columns in wide format...

EDITThe original answer with pivot_wider is WRONG, I removed it. As suggested by @stefan in the comments, it dropped a row.A proper solution would include a preliminary step to add an index column, as...

View Article

Answer by TarJae for Convert a columns into separate columns in wide format in R

Here is an approach using unnest_wider():library(purrr)library(dplyr)library(tidyr)data %>% mutate(FRUITS = map(FRUITS, ~ set_names(levels(factor(FRUITS)) == .x, levels(factor(FRUITS))))) %>%...

View Article

Answer by s_baldur for Convert a columns into separate columns in wide format...

df[c("Apple", "Banana")] <- list(df$FRUITS == "Apple", df$FRUITS == "Banana")# Gender AgeGroup EAT FRUITS Apple Banana# 1 Female 30yr_39yr Yes Apple TRUE FALSE# 2 Female 20yr_29yr Yes Apple TRUE...

View Article

Answer by stefan for Convert a columns into separate columns in wide format in R

To reshape to wide using tidyr::pivot_wider you have to add a value column to your dataset and a column with a unique id for each row:df <- data.frame( Gender = c("Female", "Female", "Female",...

View Article


Convert a columns into separate columns in wide format in R

I am trying to convert the "FRUITS" column into separate columns ("Apple" and "Banana") in wide format. Gender AgeGroup EAT FRUITS 1 Female 30yr_39yr Yes Apple 2 Female 20yr_29yr Yes Apple 3 Female...

View Article
Browsing all 6 articles
Browse latest View live