Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add column for each unique value in given row

Tags:

r

csv

dplyr

I am trying to change the format of my current data set to one that has 1 user per row, and which splits all the unique values (dynamic number of values) in the Color and Food columns into their own columns with Yes and No. Each user has a unique ID.

Current format: 
ID | Name  | Color  | Food 
1  | John  | Blue   | Pizza
1  | John  | Red    | Pizza
1  | John  | Yellow | Pizza
1  | John  | Blue   | Ice Cream
1  | John  | Red    | Ice Cream
1  | John  | Yellow | Ice Cream
2  | Kelly | Blue   | Pizza
2  | Kelly | Red    | Pizza


Desired format: 
ID | Name  | Color_Blue | Color_Red | Color_Yellow | Food_Pizza | Food_Ice Cream |
1  | John  | Yes        | Yes       | Yes          | Yes        | Yes            |
2  | Kelly | Yes        | Yes       | No           | Yes        | No             |

like image 739
manaso12 Avatar asked Dec 30 '25 16:12

manaso12


1 Answers

library(dplyr); library(tidyr)
df %>% 
  pivot_longer(-c(ID:Name)) %>%
  unite("col", c(name, value)) %>%
  distinct(ID, Name, col) %>%
  mutate(val = "Yes") %>%
  pivot_wider(names_from = col, values_from = "val", values_fill = "No")

# A tibble: 2 x 7
  ID    Name  Color_Blue Food_Pizza Color_Red Color_Yellow `Food_Ice Cream`
  <chr> <chr> <chr>      <chr>      <chr>     <chr>        <chr>           
1 1     John  Yes        Yes        Yes       Yes          Yes             
2 2     Kelly Yes        Yes        Yes       No           No   

If you want a base R equivalent, here's one using the same steps. (Can someone please help me figure out how to remove the rownames and the "val." that gets appended to the final column names?)

df2 <- reshape(df, 
        direction = "long", 
        varying = c("Color", "Food"),
        v.names = "Value",
        timevar = "col_name",
        times = c("Color", "Food"))
df2$col = paste(df2$col_name, df2$Value, sep = "_")

df3 <- unique(df2[c("ID", "Name", "col")])
df3$val = "Yes"

df4 <- reshape(df3,
               direction = "wide",
               idvar = c("ID", "Name"),
               timevar = "col")
df4[is.na(df4)] <- "No"

> df4
        ID  Name val.Color_Blue val.Color_Red val.Color_Yellow val.Food_Pizza val.Food_Ice Cream
1.Color  1  John            Yes           Yes              Yes            Yes                Yes
7.Color  2 Kelly            Yes           Yes               No            Yes                 No

sample data

df <- tribble(~ID , ~Name  , ~Color  , ~Food,
"1"  , "John",  "Blue",    "Pizza",
"1"  , "John" , "Red",    "Pizza",
"1"  , "John",  "Yellow",  "Pizza",
"1"  , "John" , "Blue",   "Ice Cream",
"1"  , "John",  "Red",    "Ice Cream",
"1"  , "John" , "Yellow", "Ice Cream",
"2"  , "Kelly", "Blue",    "Pizza",
"2"  , "Kelly", "Red",    "Pizza")
  
like image 181
Jon Spring Avatar answered Jan 02 '26 10:01

Jon Spring



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!