Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

The following is my data-frame and I am storing that in df:

df <- data.frame("Part" = c("y","z","y","z","x","y"), "Prod" = c("a","d","e","d","t","a"))

> df

    Part Prod

1    y    a

2    z    d

3    y    e

4    z    d

5    x    t

6    y    a

Now what I want is that it should look like this:

df2

   Part Prod

1    x    t

2    y    a

3    y    e

4    z    d

I want a result that summarizes results without duplication in "Part" column. How to do this is R?

1 Answer

0 votes
by (108k points)

In R programming, you  can simply use the aggregate function:

aggregate(cbind(Part, Color)~Prod, df, head, 1)

Or if you wish you can do the same thing with the help of dplyr package

library(dplyr)

df %>% group_by(Prod) %>% summarise_at(vars(Part, Color), first)

Browse Categories

...