Count frequency

#1
Hi.
Let say i have a data xdata=c(0,1,2,0,0,4). So, i want to obtain the frequency of this data including the "missing" possible value (which in this case is 3).

Basically, i want my output looks like this:
x 0 1 2 3 4
freq 3 1 1 0 1

My “half-done” R code with error:
> xdata=c(0,1,2,0,0,4)
> x=0:max(xdata)
> freq=as.data.frame(table(xdata))
> freqx=freq[,2]
> tabx=rbind(x,freqx)
Warning message:
In rbind(x, freqx) :
number of columns of result is not a multiple of vector length (arg 2)


So how to fix this so that i manage to get the desirable output?
P/S:I want the output to be automatically generated, so that i can apply it to a larger data set with a few missing value with count zero. I dont want to manually do the checking and adjusting task for every data set.

Thanks in advanced.
 
Last edited:

Dason

Ambassador to the humans
#2
You could make it a factor. Then all levels will be included even if they're empty.

Code:
> xdata <- c(0,1,2,0,0,4)
> ?factor
> x <- factor(xdata, levels = 0:4)
> table(x)
x
0 1 2 3 4 
3 1 1 0 1
 

Mike White

TS Contributor
#4
You could also use the following:
Code:
data.frame(xdata=x,freq=sapply(x, function(x,y=xdata) length(y[y==x])))
#  xdata freq
#1     0    3
#2     1    1
#3     2    1
#4     3    0
#5     4    1
 
#5
You could also use the following:
Code:
data.frame(xdata=x,freq=sapply(x, function(x,y=xdata) length(y[y==x])))
#  xdata freq
#1     0    3
#2     1    1
#3     2    1
#4     3    0
#5     4    1

Sorry Mike, i tried this suggestion but it didnt work for me. Maybe i mixed up all the x,y,xdata. I dont really know which is which. Using the following pre-defined objects, would you mind help me fix the codes?

Code:
aa=c(0,1,2,0,0,4)
bb=factor(aa, levels = 0:max(aa))
cc=table(bb)
I tried this:
Code:
data.frame(xdata=bb,freq=sapply(bb, function(bb,y=aa) length(y[y==bb])))
#  xdata freq
#1     0    3
#2     1    1
#3     2    1
#4     0    3
#5     0    3
#6     4    1
It's wrong. Hope you can help me fix this up. Thanks in advanced.
 

Mike White

TS Contributor
#6
Sorry, the original code had a line missing that defined x as 0:max(aa) and I used xdata for both the data and the column heading in the dataframe, so it should read as follows:

Code:
x<-0:max(aa)
data.frame(xdata=x,freq=sapply(x, function(x,y=aa) length(y[y==x])))
  xdata freq
1     0    3
2     1    1
3     2    1
4     3    0
5     4    1