-2

Having a long format like this:

df <- data.frame(id = c(1,1,1,2,2), text = c("stock","arrange","stock","arrange","arrange"))

How is it possible to receive the wide format of making the variables in text column new columns which have as values the number the exist? Example of expected output:

data.frame(id = c(1,2) stock = c(2,0), arrange = c(1,2))
Nathalie
  • 1,228
  • 7
  • 20
  • Also see [here](https://stackoverflow.com/q/36700028/5325862), [here](https://stackoverflow.com/q/34417973/5325862), [here](https://stackoverflow.com/q/37364249/5325862), [here](https://stackoverflow.com/q/25007317/5325862), [here](https://stackoverflow.com/q/47469412/5325862). This is a type of calculation that's been covered pretty extensively already, so it's helpful to first try looking at what posts are already out there – camille Jan 27 '20 at 21:58

2 Answers2

3

We can use table to get the frequency counts

table(df)
akrun
  • 874,273
  • 37
  • 540
  • 662
3

In case you need it as a data.frame, here's an option with data.table

library(data.table)
setDT(df)

dcast(df, id ~ text, fun.aggregate = length)
#    id arrange stock
# 1:  1       1     2
# 2:  2       2     0
IceCreamToucan
  • 28,083
  • 2
  • 22
  • 38