I have a dataset with around 700 observations with a variable, X, which has values between 1 and 19. I would like to make a table counting how many times X has the value 1, the value 2 etc.
Anyone here that has any good ideas?
Thanks!
I have a dataset with around 700 observations with a variable, X, which has values between 1 and 19. I would like to make a table counting how many times X has the value 1, the value 2 etc.
Anyone here that has any good ideas?
Thanks!
I'm sure there is a more elegant way to do this, but...
X <- floor(20*runif(700)) # generate fake data, although it will include 0's
values <- unique(X) # find possible values
counts <- rep(0,length(values)) # space to save counts
for (i in seq_along(values)){
counts[i] <- sum(X == values[i]) # take advantage that TRUE adds as 1.0
}
I think you want the table() function.
X <- sample(1:19,size = 20,replace = TRUE)
X
[1] 15 5 8 19 10 18 10 12 2 10 12 14 17 14 3 8 14 19 15 17
table(X)
X
2 3 5 8 10 12 14 15 17 18 19
1 1 1 2 3 2 3 2 2 1 2
Assuming your data is in a dataframe df (otherwise you can convert it):
dplyr::count(df, X)
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.