I have a dataset that contains an identifier variable (census_tract_number
) and a categorical variable (action_taken_name
) that contains five categories. I am trying to figure out how to create a new data frame that will break each category into a variable and show the frequency for each of these categories by census_tract_number
. I have figured out how to create a frequency variable with count but I want each category to be an individual variable with the counts underneath.
Asked
Active
Viewed 351 times
-6

Miha
- 2,559
- 2
- 19
- 34

garrettsmith
- 53
- 6
-
2Pictures of data are not helpful. It's easiest to help you if you provide a minimal [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and the desired output for that input. That was possible solutions can be tested and verified. – MrFlick Dec 07 '17 at 20:45
-
you have clearly not researched well. this is a very basic question and can be answered simply by googling "long to wide data in R" – sweetmusicality Dec 07 '17 at 22:50
1 Answers
0
library(tidyr)
df <- data.frame(census_tract_number=c(1,1,1,2,2,2,2,2,2),
action_take_name=c('a','b','c','d','a','b','e','c','f'),
freq=c(2,2,4,5,28,20,5,40,23))
results <- df %>% spread(action_take_name,freq,fill=0)
results
census_tract_number a b c d e f
1 1 2 2 4 0 0 0
2 2 28 20 40 5 5 23

Jason
- 581
- 3
- 8