0

This question is different from other solved questions on stackoverflow.

Now I have a dataframe with two columns (the race column has five possible values such as white, black, asian, etc AND the attitude column has five possible values from strongly disagree to disagree) as shown below:

race    distracted
white   agree
white   disagree
black   agree
asian   neutral
hisp    agree
indian  disagree
...     ...

Now how can I change the dataframe into the following structure with five columns but row values in the related column of attitude now will be filled in each newly built columns:

white     black      asian     hisp     indian
agree   disagree     agree     neutral  disagree
agree   disagree     neutral   agree    disagree
...     ...          ...       ...       ...

The attached image is my raw data downloaded from kaggle.

enter image description here

  • In `tidyverse` you can do `df %>% group_by(race) %>% mutate(row = row_number()) %>%pivot_wider(names_from = race, values_from = attitude)` – Ronak Shah Jun 23 '21 at 05:15
  • @RonakShah Hello, if I used the code chunk given above, RStudio shows: Error in brain5 % > % group_by(race) : could not find function "% > %" I have already loaded library(dplyr) library(tidyverse) library(tidyr) library(magrittr) – DONGPIN HU Jun 23 '21 at 07:27
  • There are extra spaces in the pipes, remove them. change `% > %` to `%>%` – Ronak Shah Jun 23 '21 at 10:13
  • How exactly is this different from other questions on reshaping long data to wide data? – camille Jun 23 '21 at 16:33

0 Answers0