【问题标题】:How to obtain unique set of rows in nested data frame?如何在嵌套数据框中获取唯一的行集?
【发布时间】:2018-11-02 18:49:28
【问题描述】:

我正在尝试在嵌套数据框中提取唯一的行集。

初始数据框如下所示:

df <- data.frame(
    hid=c(1,1,1,1,2,2,2,2,2,3,3,3,3),
    mid=c(1,2,3,4,1,2,3,4,5,1,2,3,4),
    tmid=c("010","01010","010","01020",
           "010","0120","010","010","020",
           "010","01010","010","01020"),
    thid=c("010","02020","010","02020",
           "000","0120","010","010","010",
           "010","02020","010","02020"),
    tdid=c("000","01010","010","02020",
           "000","0100","010","010","010",
           "000","01010","010","02020"),
    tiid=c("010","02020","010","01020",
           "020","0220","020","020","020",
           "010","02020","010","01020")
    )

在 dara 框架中打印:

> df
   hid mid  tmid  thid  tdid  tiid
1    1   1   010   010   000   010
2    1   2 01010 02020 01010 02020
3    1   3   010   010   010   010
4    1   4 01020 02020 02020 01020
5    2   1   010   000   000   020
6    2   2  0120  0120  0100  0220
7    2   3   010   010   010   020
8    2   4   010   010   010   020
9    2   5   020   010   010   020
10   3   1   010   010   000   010
11   3   2 01010 02020 01010 02020
12   3   3   010   010   010   010
13   3   4 01020 02020 02020 01020

此数据框由以下字段组成:

  • hid:户口本
  • mid: 家庭成员身份证
  • tmid, thid, tdid, tiid:变量表示不同定义的每个家庭成员的活动链,例如0(呆在家里)-1(在家外活动)-0(呆在家里)

现在,我需要提取家庭进行的一组独特的成员活动。我不仅要提取每个家庭中唯一的行,还需要提取集合本身。因此df %&gt;% distinct(hid,tmid,tdid,tiid) 不能提取不同的活动集,df %&gt;% distinct(hid,tmid,tdid,tiid) 不能考虑每个家庭的活动集。所需的输出如下所示:

> df.unique
   shid smid tmid  thid  tdid  tiid
1   1    1   010   010   000   010
2   1    2   010   010   010   010
3   1    3 01010 02020 01010 02020
4   1    4 01020 02020 02020 01020
5   2    1   010   000   000   020
6   2    2   010   010   010   020
7   2    3   020   010   010   020
8   2    4  0120  0120  0100  0220

与前一个数据框的区别是:

  • 删除hid==2中的重复行(删除每户重复行)
  • hid==3 被移除(移除与其他家庭相比具有完全相同活动的家庭)
  • hid 和 mid 替换为序号(家庭和成员的唯一 id 没有任何意义)

第一点和第三点可以解决,但是第二点无法通过distinct()来实现。

我假设有可能将其转换为嵌套数据框并在里面比较嵌套数据框。 目前我准备了以下嵌套数据框。

> df.nest <- df.unique %>% dplyr::select(-n) %>% group_by(hid) %>% nest()
> df.nest$data
[[1]]
# A tibble: 4 x 5
   smid   tmid   thid   tdid   tiid
  <int> <fctr> <fctr> <fctr> <fctr>
1     1    010    010    000    010
2     2    010    010    010    010
3     3  01010  02020  01010  02020
4     4  01020  02020  02020  01020

[[2]]
# A tibble: 4 x 5
   smid   tmid   thid   tdid   tiid
  <int> <fctr> <fctr> <fctr> <fctr>
1     1    010    000    000    020
2     2    010    010    010    020
3     3   0120   0120   0100   0220
4     4    020    010    010    020

[[3]]
# A tibble: 4 x 5
   smid   tmid   thid   tdid   tiid
  <int> <fctr> <fctr> <fctr> <fctr>
1     1    010    010    000    010
2     2    010    010    010    010
3     3  01010  02020  01010  02020
4     4  01020  02020  02020  01020

*sid 是新添加的,用序列号替换 mid,因为成员 ID 不再重要

有什么想法可以从嵌套列表中删除[[3]],它在[[1]] 中具有相同的数据帧(或以前显示为df.unique 的一般数据帧格式)?

由于实际的 dara 框架包含超过 60,000 个家庭,我需要不手动实现它的方法,例如 df.nest[1:2,]。其他不使用嵌套数据框的解决方案也值得赞赏。

我找到了名为 all_equal/all.equal 和 identical 的函数,但它们用于比较两个数据帧。

【问题讨论】:

  • 为什么hid==2 &amp;&amp; mid==5这一行被去掉了?
  • @user 感谢您的评论。它没有被删除,而是由shid==2 &amp;&amp; smid==3 表示,因为它是由count() 安排的。 df.unique 由 df.unique &lt;- df %&gt;% count(hid,tmid,thid,tdid,tiid) %&gt;% group_by(hid) %&gt;% mutate(smid=row_number()) %&gt;% dplyr::select(hid,smid,tmid,thid,tdid,tiid,n) 创建(hid 在此脚本中未被 shid 重命名)
  • 那么您是否要同时删除重复的组以及组内组中的重复行?
  • @uerR 是的,没错。

标签: r list dataframe type-conversion tidyverse


【解决方案1】:

函数 !duplicated 可以删除重复的行。 例子: df [!duplicated(df[c("hid","mid", "tmid" , "thid", "tdid" , "tiid")]),]

【讨论】:

  • 谢谢,但hid 可能无法区分具有完全相同记录的每个家庭。如果从脚本中删除hid,则它无法识别每个家庭中的记录对。这是我正在解决的问题。
猜你喜欢
  • 2017-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-07
  • 2020-11-23
  • 2021-05-31
相关资源
最近更新 更多