smihael
smihael

Reputation: 923

Merging rows if values in a specific column are identical

Given the following data

Cues        Outcome   Freq
items_a     s         60
item_a      rat       100
items_a     rats      60

How can I merge the two rows if both $Cues and $Freq are identical to get:

Cues        Outcome   Freq
items_a     s_rats    60
item_a      rat       100

(i.e. append the value of cell in $Outcome column from the row to be merged)

All I managed so far was to remove the row, but not to merge data. newtable = subset(table, Outcomes == "s" | Outcomes == "rat" )

Upvotes: 0

Views: 98

Answers (1)

Julián Urbano
Julián Urbano

Reputation: 8488

> aggregate(Outcome~Cues+Freq, d, function(x) paste(collapse="_", x))
     Cues Freq Outcome
1 items_a   60  s_rats
2  item_a  100     rat

where d is your dataframe.

Upvotes: 1

Related Questions