r - table frequencies with custom column -
for example, have matrix:
a <- c(2,3,4,3,3,2,2) table(a)
output:
2 3 4 3 3 1
but output want is:
1 2 3 4 0 3 3 1
so example, value range 1 4.... because of random value, list may doesnt contain 1 or more element range when value range 1 4, want table have 4 coloumns not exist value filled 0 (0)
you can convert 'a' 'factor' , specify levels
before doing table
table(factor(a, levels=seq_len(max(a)))) #1 2 3 4 #0 3 3 1
if need custom level
should specified well. using example comments,
a<- c(2,3,3,3,2,2) table(factor(a, levels=1:4)) #1 2 3 4 #0 3 3 0
Comments
Post a Comment