如何通过消除重复观察来对我的数据进行子集化 [英] How to subset my data with eliminating repeated observations

查看:16
本文介绍了如何通过消除重复观察来对我的数据进行子集化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何消除对 IGM 的重复观察?我想把以下数据做成一个县一个IGM.

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

我试过了

    data$GM[data$county]

但它没有用,因为我需要在 [] 里面有一个行号,而不是一个县号.一县如何匹配一名GM?

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

到以下数据.

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

谢谢.

推荐答案

您可以使用 duplicated 函数获取每个县的第一个观测值:

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

这篇关于如何通过消除重复观察来对我的数据进行子集化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆