Reputation: 177
I've worked with table(vector) to see what the frequency is of each element from my vector. But now I want to use the frequency of a number. I've tried the following code but it didn't work:
table(vector)[2]
Then I get for example
7
2
So the number 7 attend 2 times in the vector. But I want to work with the "2", how do I do that?
Silke
Upvotes: 0
Views: 61
Reputation: 2032
Lets say we have the following
vector <- sample(c(5:10),100,replace=T)
> table(vector)
vector
5 6 7 8 9 10
22 17 20 14 10 17
The first row is just the names and the bottom row is the values so if you do
x <- table(vector)
str(x)
'table' int [1:6(1d)] 22 17 20 14 10 17
- attr(*, "dimnames")=List of 1
..$ vector: chr [1:6] "5" "6" "7" "8" ...
you get the actual frequency values itself in a vector with names. Why are you not able to work with this? Can you give an example of the problem you are facing.
Upvotes: 0
Reputation: 121568
tt <- table(vector)
To get the frequency of "7" (column name here) , you can do this:
tt[["7"]]
You can also transform your table to a data.frame:
dat <- as.data.frame(tt)
dat[dat$Var1==7]$Freq
Upvotes: 2