Reputation: 39
I have a question how to solve a problem more nicely.
I want to define a new column, based on the highest number of another column after grouping by another column. I did found a solution, see below, but I assume there are better ways to do this.
I want the totaltestno of each SN to be the highest testno of that particular SN.
Thanks for any help and suggestions!
sets <- data.frame(SN=c(1234, 1234, 2345, 3456, 7890, 7890, 7890, 1234, 1234, 2345, 3456, 7890, 7890, 7890), a= c(1,2, 3, 4, 1,2, 1,2,1,2,3,4,5,6) ,testno=
c(1,1,2,2,1,1,1,1,1,1,2,2,3,3))
sets
sets <- sets %>% group_by(SN) %>%
arrange(desc(testno), .by_group = TRUE)
setshighestno <- sets %>% distinct(SN, .keep_all = TRUE)
setshighestno$totaltestno <- setshighestno$testno
setshighestno$testno <- NULL
setshighestno$a <- NULL
setshighestno
newsets <- merge(x = sets, y=setshighestno, by = "SN")
newsets
Upvotes: 1
Views: 36
Reputation: 887881
We can use
library(dplyr)
newsets %>%
group_by(SN) %>%
mutate(totaltestno2 = max(testno))
Upvotes: 1