How to subset my data with eliminating repeated observations

偶尔善良 提交于 2019-12-13 04:25:03

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/22927273/how-to-subset-my-data-with-eliminating-repeated-observations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!