0

I have a data.frame like the following:

 GROUP OGS valMax
1 Group1 A   81.5
2 Group1 B   87.1
3 Group1 C   66.2
4 Group2 D   7.2
5 Group2 E   5.3
6 Group2 F   10.1

I am trying to create new column "groupMax" by filling it with max of "valMax". The expected output is:

  GROUP OGS valMax groupMax
1 Group1 A   81.5  87.1
2 Group1 B   87.1  87.1
3 Group1 C   66.2  87.1
4 Group2 D   7.2   10.1
5 Group2 E   5.3   10.1
6 Group2 F   10.1  10.1

I did the following:

library(tidyverse)
library(magrittr)
df %>% group_by(GROUP) %>% mutate(groupMax = max(valMax))

The error I get is,

Error in mutate(groupMax = max(valMax)) : object 'valMax' not found

Is the data masking not working here?

Thanks in advance.

Arun
  • 649
  • 8
  • 24
  • 2
    `groupby` should be `group_by`. Apart from that your code looks correct. Are you sure column is called `valMax` ? What does `names(valMax)` return? – Ronak Shah Mar 09 '21 at 07:43
  • Please share your data in [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (like a `dput`) so we can just copy/paste it for testing. Something doesn't seem right about what you posted. You should have got a different error message. – MrFlick Mar 09 '21 at 07:44

1 Answers1

0

As Ronak suggests: You can group_by GROUP and then mutate a new variable groupMaxwith max()function

library(dplyr)

# your data
df <- tribble(

  ~GROUP, ~OGS, ~valMax,
"Group1", "A", 81.5, 
"Group1", "B", 87.1, 
"Group1", "C", 66.2, 
"Group2", "D", 7.2, 
"Group2", "E", 5.3, 
"Group2", "F", 10.1)

df1 <- df %>% 
  group_by(GROUP) %>% 
  mutate(groupMax = max(valMax))

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • 1
    Isn't this what the OP was already doing? Do you think the problem was using `groupby` rather than `group_by`? Shouldn't have have given a different error message? Or are you assuming `groupby` was another function that was defined in the global workspace? – MrFlick Mar 09 '21 at 07:47
  • @ MrFlick. Yes you are right `groupby` was the problem. I saw the desired output and started to solve. I recognized it after I posted my answer. Should I delete the answer? – TarJae Mar 09 '21 at 07:51
  • 1
    Yes. I mixed it with pandas and made a typo. Also, in my end, I got the pipe-symbol wrong. Instead of %>%, it read %%. – Arun Mar 09 '21 at 08:24