I'm trying to turn a dataframe from long to wide format. Currently the df is set up like so:
dput(head(df,10))
structure(list(TECH_ID = c("14050154", "14050154", "13835650",
"13835650", "13469601", "13469601", "13782883", "13782883", "12342837",
"12342837"), MNSCU_QUES = c("What language did you learn to speak first?",
"Which language do you speak most often at home?", "What language did you learn to speak first?",
"Which language do you speak most often at home?", "What language did you learn to speak first?",
"Which language do you speak most often at home?", "What language did you learn to speak first?",
"Which language do you speak most often at home?", "What language did you learn to speak first?",
"Which language do you speak most often at home?"), MNSCU_RESP = c("English and another language",
"Another", "English only", "English", "English only", "English",
"English and another language", "English", "English only", "English"
)), row.names = c(NA, 10L), class = "data.frame")
I am trying to set up the dataframe so it reads like this:
I've been trying to use this code here:
df_wide <- dcast(df, TECH_ID+MNSCU_RESP~MNSCU_QUES)
But the resulting dataframe looks like this:
Code:
dput(head(df_wide,10))
structure(list(TECH_ID = c("00007179", "00007179", "00008201",
"00008201", "00020900", "00020900", "00021757", "00021757", "00031227",
"00031227"), MNSCU_RESP = c("English", "English only", "English",
"English only", "English", "English only", "English", "English only",
"English", "English only"), `What language did you learn to speak first?` = c(0L,
1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L), `Which language do you speak most often at home?` = c(1L,
0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L)), row.names = c(NA, 10L), class = "data.frame")