0

I will build on this question Similar to Remove Rows From Data Frame where a Row match a String

For example:

A,B,org.id
4,3,Foo
2,3,Bar
2,4,Bar
7,5,Zap
7,4,Zap
7,3,Zap

How would I return a dataframe that excludes all rows where org.id is the same as the row above?

A,B,org.id
4,3,Foo
2,3,Bar
7,5,Zap

Guess: Perhaps melt() or cast() functions could do the trick. (I only know how to do this in excel, where I have to create a new dataframe and do IF[a2=a1,0,a2].)

The question is also similar to Subtract the previous row of data where the id is the same as the row above but that is in sql.

Community
  • 1
  • 1

1 Answers1

1

You can try with duplicated

 df1[!duplicated(df1$org.id),]
 #   A B org.id
 #1 4 3    Foo
 #2 2 3    Bar
 #4 7 5    Zap

Or using unique with by option

 library(data.table)
 unique(setDT(df1), by='org.id')
akrun
  • 874,273
  • 37
  • 540
  • 662
  • I tried **head((dff_all2[duplicated(dff_all2$id), c(1,4,6,17)]))** # but it outputs: country_id id device_model channel 177 BR 776962 iPhone5,1 (iPhone 5) Organic 189 BR 687574 GT-I9505 samsung SEM 310 BR 1037119 XT1058 motorola Organic 439 BR 735454 GT-I8190L samsung Organic 805 BR 779030 GT-S5360B samsung Direct 852 BR 661698 iPhone3,1 (iPhone 4 GSM) Direct – jacob Jul 22 '15 at 10:00
  • @jacob My code was based on the example you showed. Please update your post with a new example (preferably with `dput`) that causes the anomaly. – akrun Jul 22 '15 at 10:14