Demo
Demo

Reputation: 301

Return the largest several numbers as a list of a data frame in r

I have dataset call df, like this:

Title Time   Gross
a     12/21   2313
b     11/12   1298
c     4/09    10034
d     09/30   3498
e     10/31   2375
y     01/10   8492 

I want write a function, return the Title and Gross of 3 entries with largest Gross.
I tried like this:

names.revenues <- function(num, data){
  i = 1
  for(i in 1:num){
    inds = which.max(data$Gross)
    names[i] = data$Title[inds]
    revenues[i] = data$Gross[inds]
    data = data[-inds, ]
    i = i + 1
  }
  return(list(names = names, revenues = revenues))
}
names.revenues(3, df)

But this doesn't work, please give me a hint. Thanks!

Upvotes: 0

Views: 49

Answers (1)

digEmAll
digEmAll

Reputation: 57210

Your function has some mistakes, here's a possible working version:

names.revenues <- function(num, data){
  # initialize names and revenues with NAs
  names <- rep.int(NA,num)
  revenues <- rep.int(NA,num)

  for(i in 1:num){
    inds = which.max(data$Gross)
    names[i] = data$Title[inds]
    revenues[i] = data$Gross[inds]
    data = data[-inds, ]
  }
  return(list(names = names, revenues = revenues))
}

Example :

df <- data.frame(Title=letters[1:10],
                 Time=format(format='%m/%d',Sys.Date()+1:10),
                 Gross=c(10,200,40,500,70,500,20,300,10,50),
                 stringsAsFactors=FALSE)
# > df
#    Title  Time Gross
# 1      a 10/30    10
# 2      b 10/31   200
# 3      c 11/01    40
# 4      d 11/02   500
# 5      e 11/03    70
# 6      f 11/04   500
# 7      g 11/05    20
# 8      h 11/06   300
# 9      i 11/07    10
# 10     j 11/08    50

res1 <- names.revenues(5, df)
# > res 1
# $names
# [1] "d" "f" "h" "b" "e"
# 
# $revenues
# [1] 500 500 300 200  70

But is possible to obtain basically the same result with this (more efficient) one-liner :

res2 <- head(df[order(df$Gross,decreasing=TRUE),],5)
# > res2
#   Title  Time Gross
# 4     d 11/02   500
# 6     f 11/04   500
# 8     h 11/06   300
# 2     b 10/31   200
# 5     e 11/03    70

Upvotes: 2

Related Questions