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

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

$
0
0

EDIT

The 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 already shown in @TarJae's answer, to which I should only add the simplification values_fn = ~ TRUE.The dummy_cols suggestion stands as a valid alternative

If a one-hot encoding/dummy presentation of values is OK (1s and 0s instead of logical), fastDummies::dummy_cols is nice too:

library(fastDummies)df |>     dummy_cols('FRUITS',               remove_selected_columns = TRUE,               omit_colname_prefix = TRUE)  Gender  AgeGroup EAT Apple Banana1 Female 30yr_39yr Yes     1      02 Female 20yr_29yr Yes     1      03 Female 70yr_80yr Yes     1      04   Male 50yr_59yr Yes     0      15 Female 40yr_49yr Yes     1      06 Female 70yr_80yr Yes     1      0

Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>