I have a dataset with large number of observations. The follow dataframe is just a smaller version of it. the dput output is given below
structure(list(Tags = c(1, 1, 3, 3, 3, 1), County = c("USA",
"UK", "USA", "UK", "USA", "UK")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
I want R to display the top 10 numbers that appear and the number of times they appear. Currently i am using the following code
table(Data$Tags)
While my dataset has a lot many observations than this one hence I want the top ten.
CodePudding user response:
You may apply sort()
and head()
to the output of table()
, e.g.,
head(sort(table(Data$Tags), decreasing=TRUE), 10)
CodePudding user response:
Here is dplyr
version using slice_max
with an example dataset:
library(dplyr)
set.seed(1)
as_tibble(sample(1:10, size = 300, replace = TRUE)) %>%
count(value, sort = TRUE, name="myCount") %>%
slice_max(myCount, n=10)
value myCount
<int> <int>
1 7 39
2 10 36
3 3 33
4 9 33
5 8 31
6 6 30
7 4 29
8 2 25
9 5 25
10 1 19