Reputation: 59
I wrote a function in R which is supposed to return the first five developers who made the most input:
developer.busy <- function(x){
bus.dev <- sort(table(test2$devf), decreasing = TRUE)
return(bus.dev)
}
bus.dev(test2)
ericb shields mdejong cabbey lord elliott-oss jikesadmin coar
3224 1432 998 470 241 179 77 1
At the moment it just prints out all developers sorted in decreasing range. I just want the first 5 to be shown. How can I make this possible. Any suggestion is welcome.
Upvotes: 1
Views: 60
Reputation: 887098
If we want the first five, either use index with [
or with head
. Modified the function with three input, data object name, column name ('colnm') and number of elements to extract ('n')
developer.busy <- function(data, colnm, n){
sort(table(data[[colnm]]), decreasing = TRUE)[seq_len(n)]
# or another optioin is
head(sort(table(data[[colnm]]), decreasing = TRUE), n)
}
developer.busy(test2, "developerf", n = 5)
-using a reproducible example with mtcars
dataset
data(mtcars)
developer.busy(mtcars, 'carb', 5)
# 2 4 1 3 6
#10 10 7 3 1
Upvotes: 2