Long to Wide Dataset while adding new variable

Hello, I am trying to change my data from long to wide. I have an example of what it could look like

Instead, I want to see it as a wide and I want to create a new column T/F column to see if they have ever traveled to a certain continent and I want to make sure I can count how many Ts and Fs, like this:

I tried to do something like with group_by and mutate but I was definitely doing something wrong.
A = original long wide dataset

A$Asia = asia %>%
group_by(ID) %>%
mutate(Asia = {if(Continent='Asia') T else F})

What is the best way to do this?

~~ Reproducible example ~~

data.frame(
  stringsAsFactors = FALSE,
                               ID = c(1L,1L,1L,2L,2L,2L,2L,3L,3L,4L,4L,
                                      4L,4L,4L),
                        Continent = c("Asia","Asia","Africa","South America",
                                      "Asia","Africa","Europe",
                                      "North America","Europe","Africa","South America",
                                      "Europe","Africa","Asia")
               )

Can you provide a reproducible example instead of a screenshot?

data.frame(
  stringsAsFactors = FALSE,
                               ID = c(1L,1L,1L,2L,2L,2L,2L,3L,3L,4L,4L,
                                      4L,4L,4L),
                        Continent = c("Asia","Asia","Africa","South America",
                                      "Asia","Africa","Europe",
                                      "North America","Europe","Africa","South America",
                                      "Europe","Africa","Asia")
               )

(hope I'm doing this right)

1 Like

This works:

library(tidyverse)

df %>% 
  mutate(visited = TRUE) %>% 
  pivot_wider(ID, names_from = Continent, values_from = visited) %>% 
  unnest() %>% 
  distinct(ID, .keep_all = TRUE) %>% 
  replace(is.na(.), FALSE)

# A tibble: 4 x 6
     ID Asia  Africa `South America` Europe `North America`
  <int> <lgl> <lgl>  <lgl>           <lgl>  <lgl>          
1     1 TRUE  TRUE   FALSE           FALSE  FALSE          
2     2 TRUE  TRUE   TRUE            TRUE   FALSE          
3     3 FALSE FALSE  FALSE           TRUE   TRUE           
4     4 TRUE  TRUE   TRUE            TRUE   FALSE  
1 Like
df %>% 
  distinct() %>%
  mutate(visited = TRUE) %>%
  pivot_wider(names_from = Continent, values_from = visited) %>%
  mutate_all(replace_na, FALSE)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.