Questioneer
Questioneer

Reputation: 15

Delete the lower value in one column based on repeat values in another column in R (large data set)

I have a large data set loaded into R that contains multiple duplicates in one column (colA) and another column that has different unique values (colB). I need to figure out a way delete the lowest values in colB that correspond to the same value in colA.

For example,

A 1 
A 2
A 3
B 8
B 9
B 10

should become

A 3
B 10

If this were something like Python, it would be an easy command to code, but I am new to R and greatly appreciate the help.

Upvotes: 0

Views: 48

Answers (3)

rgunning
rgunning

Reputation: 568

library(plyr)
data<-data.frame("x"=c(rep("A",3),rep("B",3)),"y"=c(1:3,8:10))
ddply(data,~x,summarise,max=max(y))

  x max
1 A   3
2 B  10

Upvotes: 0

Daniel Anderson
Daniel Anderson

Reputation: 2424

Here's a dplyr solution

d <- read.table(textConnection("A 1 
A 2
A 3
B 8
B 9
B 10"))

library(dplyr)
d %>% 
    group_by(V1) %>% 
    summarize(max = max(V2))

# A tibble: 2 × 2
      V1   max
  <fctr> <int>
1      A     3
2      B    10

Upvotes: 2

G5W
G5W

Reputation: 37641

You can do this with aggregate

aggregate(df$B, list(df$A), max)
  Group.1  x
1       A  3
2       B 10

Upvotes: 1

Related Questions