1

I have a df called "homes", it has two columns: "Education level" and "Insurance". Ed level is a categoric variable has four leves: Primary, High-Sch, Terciary and College. For instance:

Ed.Level. Insurance
Primary.    YES
Primary.    NO
Primary.    NO
College.    YES
College.    YES
High-Sch.   YES
High-Sch.   NO
Terc.       YES

I need to create a new column with name: Primary with values: YES, NO, etc. and the same logic for each education level. The expected output would be:

Primary. Secondary  High Sch  College
YES.       NO.        YES.     NO
NO.        YES.       NO.      YES
YES.       YES        YES.     NO
NO.        YES        YES.     YES

The values in the expected output are refered to the Insurance field.

I tried with a transpose and didn't work, cbind, merges, but nothing worked. How can I do that? Thanks.

AgusDam87
  • 97
  • 7
  • 1
    You need `library(dplyr);library(tidyr);library(data.table); df1 %>% mutate(rn = rowid(Ed.Level.)) %>% pivot_wider(names_from = Ed.Level., values_from = Insurance)` – akrun Jun 11 '21 at 21:25

1 Answers1

2

We can use pivot_wider to reshape into 'wide' format after creating a sequence column with rowid to take care of duplicate rows

library(dplyr)
library(tidyr)
library(data.table)
df1 %>%
    mutate(rn = rowid(Ed.Level.)) %>% 
    pivot_wider(names_from = Ed.Level., values_from = Insurance) %>%
    select(-rn)

data

df1 <- structure(list(Ed.Level. = c("Primary.", "Primary.", "Primary.", 
"College.", "College.", "High-Sch.", "High-Sch.", "Terc."), Insurance = c("YES", 
"NO", "NO", "YES", "YES", "YES", "NO", "YES")), 
class = "data.frame", row.names = c(NA, 
-8L))
akrun
  • 874,273
  • 37
  • 540
  • 662