johnsinclair
johnsinclair

Reputation: 109

R: How to merge two rows based on specific values

given the example table,

  ID T A B X Y Z
   1 S 1   
   2 S 2
   1 E   4 a b c
   3 S 5
   2 E   8 d e f 

and the assumptions:

  1. for the same ID there is a pair of rows (first row T == S; second row T == E)
  2. in the first row (T == S) the columns ID, T, A have values
  3. in the second row (T == E) the columns ID, T, B, X, Y, Z have values
  4. the two row pairs are not necessarily below each other

I try to do the following:

  1. Look for rows with the same ID
  2. and merge the values (into the row T == S)
  3. remove rows with T == E // since merged with other row

The result would look like this

  ID T A B X Y Z
   1 S 1 4 a b c   
   2 S 2 8 d e f
   3 S 5
   ...

Currently I use two nested for-loops, which is too slow. Has anybody a idea that is faster than two nested for-loops?

Upvotes: 0

Views: 815

Answers (1)

Sayali Sonawane
Sayali Sonawane

Reputation: 12609

Combine rows and sum their values refer answer section of this question.

Upvotes: 1

Related Questions