Keshav M
Keshav M

Reputation: 1349

Create frequency vector based on input vector

I have a variable test in the structure:

> test <- c(9,87)
> names(test) <- c("VGP", "GGW")
> dput(test)
structure(c(9, 87), .Names = c("VGP", "GGW"))
> class(test)
[1] "numeric"

This is a very simplified version of the input vector, but I want an output as a vector of length 100 which contains the frequency of each number 1-100 inclusive. The real input vector is of length ~1000000, so I am looking for an approach that will work for a vector of any length, assuming only numbers 1-100 are in it.

In this example, the numbers in all positions except 9 and 87 will show up as 0, and the 9th and 87th vector will both say 50.

How can I generate this output?

Upvotes: 1

Views: 156

Answers (3)

R Thomas
R Thomas

Reputation: 156

If you have a numeric vector and just want to get a frequency table of the values, use the table function.

set.seed(1234)
d <- sample(1:10, 1000, replace = TRUE)
x <- table(d)
x
#  1   2   3   4   5   6   7   8   9  10   
# 92  98 101 104  87 112 104  94  88 120 

If there is a possibility of missing values, say 11 is a possibility in my example then I'd do the following:

y <- rep(0, 11)
names(y) <- as.character(1:11)
y[as.numeric(names(x))] <- x
y
# 1   2   3   4   5   6   7   8   9  10  11 
 92  98 101 104  87 112 104  94  88 120   0 

Upvotes: 0

akrun
akrun

Reputation: 887691

If we are looking for a proportion inclusive of the values that are not in the vector and to have those values as 0, convert the vector to factor with levels specified and then do the table and prop.table

100*prop.table(table(factor(test, levels = 1:100)))

Upvotes: 3

Aishwarya
Aishwarya

Reputation: 110

>freq<-vector(mode="numeric",length=100)
>for(i in X)
+{ if(i>=1 && i<=100)
+  freq[i]=freq[i]+1
+}
>freq

X is the vector containing 10000 elements Adding an if condition could ensure that the values are in the range of [1,100].

Hope this helps.

Upvotes: 1

Related Questions