0

I'm trying to replicate the max if function from Excel in R.

ksu       price   max
9144037   3.11    3.11
8448749   4.19    5.24
9649391   0       8.39
8448749   4.19    5.24
8448749   4.19    5.24
8448749   4.19    5.24
8448749   4.19    5.24
9649391   8.39    8.39
8448749   5.24    5.24
9144037   1.99    3.11
9144037   1.99    3.11

If I were doing it in excel i'd use max(if()). This code is supposed to look at the max price for each ksu, and return the max value on the last column. I've tried this :

max(price[ksu == ksu])

But it doesn't give me the desired output. It only returns one max value regardless of the ksu.

user1834217
  • 39
  • 1
  • 8
  • For future reference please read the following before submitting any more questions: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Dason Apr 26 '17 at 16:49

2 Answers2

2

Assuming you have a data.frame called df you could easily use the ave function to get what you want. An example:

> df <- data.frame(grp = c('a','a','b','b'), vals = 1:4)
> df
  grp vals
1   a    1
2   a    2
3   b    3
4   b    4
> # Returns a vector
> ave(df$vals, df$grp, FUN = max)
[1] 2 2 4 4
> # So we can store it back into the data.frame if we want
> df$max <- ave(df$vals, df$grp, FUN = max)
> df
  grp vals max
1   a    1   2
2   a    2   2
3   b    3   4
4   b    4   4

So using your variable names (but still assuming the data.frame is df):

df$max <- ave(df$price, df$ksu, FUN = max)
Dason
  • 60,663
  • 9
  • 131
  • 148
  • Another option is `setDT(df)[, Max := max(vals), grp]` – akrun Apr 26 '17 at 16:46
  • @akrun Right. I was going for a base solution but you can submit a data.table solution if you want. – Dason Apr 26 '17 at 16:48
  • You can add it as it is very simple – akrun Apr 26 '17 at 16:50
  • Sure it's simple but it's a different approach. No harm in having multiple answers. Honestly the question probably could be closed as a dupe of *something* but I'm too lazy to find the dupe. – Dason Apr 26 '17 at 16:50
1

Suppose your data is in a data.frame called dat, we can use the dplyr package:

library(dplyr)
dat %>%
  group_by(ksu) %>%
  mutate(max = max(price))

       ksu price   max
     <int> <dbl> <dbl>
1  9144037  3.11  3.11
2  8448749  4.19  5.24
3  9649391  0.00  8.39
...
bouncyball
  • 10,631
  • 19
  • 31