0

I have data like this :

Code  X    Y   Z
1     12.2 3.3 3.1
1     125  2   1
1     89   26  03
2     54   15  26 
2     89   26  65
3     26   56  15
3     4    2   5 
3     1    1   1
3     1    2   1

I want first row for each code. For example, I want something like this:

Code  X     Y    Z
1     12.2  3.3  3.1
2     54    15   26  
3     1     2    1

I know about unique but I donot know how it gets value, so I can not rely on this function. I request you please try to answer within the base packages available in the R.

Thanks

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Neeraj
  • 1,166
  • 9
  • 21

1 Answers1

2

Try

 df[match(unique(df$Code),df$Code),]

# Code    X    Y    Z
# 1    1 12.2  3.3  3.1
# 4    2 54.0 15.0 26.0
# 6    3 26.0 56.0 15.0

match returns a vector of the positions for the first matches, which is what interest you here.

etienne
  • 3,648
  • 4
  • 23
  • 37