Reputation: 9765
I am trying to get a frequency table using just base R (because my version does not support magrittr). I tried a few things, but nothing was what I needed. Any ideas?
data<-data.frame(a=c(1,2,3), b=c(9,9,9), c=c(12,3,4) )
#table(data)
#prop.table(data)
Goal
1 2 3 4 9 12
a 1 1 1 0 0 0
b 0 0 0 0 3 0
c 0 0 1 1 0 1
Upvotes: 1
Views: 105
Reputation: 13139
Or completely in base-R:
#edit colnames to work with base reshape
colnames(data) <- paste0("obs.",colnames(data))
m_data <- reshape(data,direction="long", varying=1:3)
> with(m_data, table(time, obs))
obs
time 1 2 3 4 9 12
a 1 1 1 0 0 0
b 0 0 0 0 3 0
c 0 0 1 1 0 1
Upvotes: 2
Reputation: 66819
You could convert the data to long format before table
-ing:
library(reshape2)
table(melt(data))
# value
# variable 1 2 3 4 9 12
# a 1 1 1 0 0 0
# b 0 0 0 0 3 0
# c 0 0 1 1 0 1
library(reshape2)
should be available on CRAN going back many R versions.
Upvotes: 1