user3077008
user3077008

Reputation: 847

How to subset my data with eliminating repeated observations

How can I erase repeated observations of IGM? I want to make following data as one IGM per one county.

I tried

    data$GM[data$county]

But it didn't work, because I need a row number inside [], not a county number. How can I match one GM per one county?

To be clear, I want to make this data

   county cd110 repvote   state  GM  gini
2    1001   102       1 Alabama 38.4 0.381
3    1001   102       1 Alabama 38.4 0.381
4    1003   101       0 Alabama 39.6 0.491
5    1003   101       0 Alabama 39.6 0.491
9    1003   101       0 Alabama 39.6 0.491
13   1003   101       1 Alabama 39.6 0.491

to following data.

  county cd110 repvote   state  GM  gini
   1001   102       1 Alabama 38.4 0.381
   1003   101       0 Alabama 39.6 0.491

Thank you.

Upvotes: 0

Views: 46

Answers (1)

josliber
josliber

Reputation: 44309

You can use the duplicated function to get the first observation for each county:

dat[!duplicated(dat$county),]
#   county cd110 repvote   state   GM  gini
# 2   1001   102       1 Alabama 38.4 0.381
# 4   1003   101       0 Alabama 39.6 0.491

Upvotes: 1

Related Questions