aprelude
aprelude

Reputation: 43

Rstudio: Combining row values into one row based on duplicate values in column

enter image description here

I screenshotted part of my table that I'm using in Rstudio. I want to combine the rows that have the same middle column value like "01020" and combine the 3rd columns' values together into one so the 01020 row would be 2 instead of 1. I don't care about retaining the 1st column. How would I do this?

Upvotes: 1

Views: 46

Answers (2)

Kian
Kian

Reputation: 110

you can use (reshape2)

df2 <- dcast(df1, col1 ~ col2, value.var = "col3", fun.aggregate = sum)

Upvotes: 1

akrun
akrun

Reputation: 886938

We can use aggeregate

aggregate(col3 ~ col2, df1, FUN = sum)

Upvotes: 1

Related Questions