-1

I have a dataframe as shown here with a list for person 1 under the "French" column.

enter image description here

under the "French" and "Chinese" Columns, 1 represents "everyday", 2 represents "several times a week", 3 represents "rarely".

However, the ideal data transformation is to create multiple rows instead of creating 1 row for each person containing all their "how often" points as shown here

enter image description here

Phil
  • 7,287
  • 3
  • 36
  • 66
fygeng go
  • 15
  • 4
  • Please share a sample of your data set, it will help answer your question. Please see these guidelines to create a minimal reproducible example https://stackoverflow.com/help/minimal-reproducible-example. – Grzegorz Sapijaszko Jan 27 '22 at 17:57

1 Answers1

0
library(dplyr)
library(tidyr)

df %>% 
  unnest(c(French, Chinese)) %>% 
  mutate(Food_type = case_when(French==1 ~ "everyday",
                              French==2 ~ "several times a week",
                              French==3 ~ "rarely"),
         how_often = case_when(Chinese==1 ~ "everyday",
                               Chinese==2 ~ "several times a week",
                               Chinese==3 ~ "rarely"))
     ID French Chinese Food_type            how_often
  <int>  <dbl>   <dbl> <chr>                <chr>    
1     1      1       3 everyday             rarely   
2     1      3       3 rarely               rarely   
3     1      2       3 several times a week rarely   
4     2     NA       3 NA                   rarely 

data:

df <- tibble(ID = 1:2,
             French = list(c(1,3,2),NULL),
             Chinese = c(3,3)
             )
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Hi @Targjae, the question's phrasing may be a little unclear. Please take a look at my initial question here https://stackoverflow.com/questions/70735462/how-to-transform-the-dataframe – fygeng go Jan 17 '22 at 05:40
  • @fygenggo Fygeng go: It might also help @TarJae if you edit your question and provide your data.frame using `dput`, something like `dput(head(df))`... – Ben Jan 17 '22 at 13:01