0

I need to find the minimum of following:

  Product Year
   N1     1988
   N2     1986
   N1     2008
   N1     2008
   N2     1999
   N2     2007

I want to find which year product first listed using RScript. For example product N1 was first listed in the year 1988. Product N2 was first listed in 1986. Similarly I have to do for 500000 products.

Thomas
  • 43,637
  • 12
  • 109
  • 140
Rehoboth
  • 43
  • 2
  • 6

2 Answers2

3

There are different possbilities: aggregate, tapply, or by.

dat <- read.table(text = " Product Year
   N1     1988
   N2     1986
   N1     2008
   N1     2008
   N2     1999
   N2     2007", header = TRUE)

aggregate(Year ~ Product, dat, min)
  Product Year
1      N1 1988
2      N2 1986

with(dat, tapply(Year, Product, min))
  N1   N2 
1988 1986 

with(dat, by(Year, Product, min))
Product: N1
[1] 1988
-------------------------------------------------------------------------------- 
  Product: N2
[1] 1986
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • `library(plyr); ddply(dd,"Product",summarise,minYear=min(Year))` – Ben Bolker Jul 21 '13 at 15:46
  • Just curious: do you see a reason to prefer one of these? (Alternatively, I could ask as a new question, if that's better.) – gung - Reinstate Monica Jul 21 '13 at 15:56
  • I believe there are a variety of questions already on SO that do benchmarking on these options, plus `data.table` solutions, e.g. http://stackoverflow.com/questions/14048739/r-use-ddply-or-aggregate/14048849#14048849 – Ben Bolker Jul 21 '13 at 16:12
1

This is pretty trivial to do in R. Try:

dframe = read.table(text=" Product Year
   N1     1988
   N2     1986
   N1     2008
   N1     2008
   N2     1999
   N2     2007", header=T)

lapply(split(dframe$Year, dframe$Product), FUN=min)
$N1
[1] 1988

$N2
[1] 1986

The key functions here are ?split and ?lapply. split() breaks up the data frame by the product groups and returns a list of lists. lapply() applies the function min() to each of those lists, and returns the output indexed by the list (product) in question.

gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79