【问题标题】:Assign a single ID from multiple observations从多个观察中分配一个 ID
【发布时间】:2013-07-23 01:26:31
【问题描述】:

我有一个调查数据集,其中受访者多次出现。受访者由 ID 标识。除此之外,还有第二个变量 (IDPREV),它给出了之前识别受访者的 ID(在上次采访中分配给受访者的 ID)。我想为每个受访者创建一个唯一的 ID,每次采访受访者时都保持不变。受访者可以在数据集中多次出现。任何建议和想法表示赞赏!谢谢!

结构看起来像这样。不幸的是,它只是没有那么好组织,并且受访者可能会出现多次:

structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, NA, NA, NA, NA, NA, 
1, 2, 3, 4, 5), .Dim = c(10L, 2L), .Dimnames = list(NULL, c("ID", 
"IDPREV")))

【问题讨论】:

  • 你好,一个简单的dput(<your data>) 会很有帮助。你的数据现在是什么样子的? (结构是什么?你有它在 R 或其他地方等)
  • 你需要解释为什么第一个提到的ID是不够的。如果这些是 R 数据对象中的数据,您需要提供来自:dput(head(object)) 的输出。如果不是,那么这真的不是编程问题,是吗?
  • ID 不够,因为它是连续的。三年后,我获得了第二个身份 (IDPREV),它指的是受访者的最后一次观察;数据存储为数据集。由于数据集太长,我无法为您提供原始数据,我必须对其进行分层,并且它是机密的。

标签: r


【解决方案1】:

这样的事情应该很快收敛:

x      <- ifelse(is.na(df$IDPREV), df$ID, df$IDPREV)
update <- function(x) x[match(x, df$ID)]
y      <- update(x)

while(!identical(y, x)) {
  x <- y
  y <- update(x)
}

df$FINAL_ID <- x

测试:

df <- structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), 
                     IDPREV = c(NA, NA, NA, NA, NA, 1, 2, 3, 4, 5, 10, 11)),
                .Names = c("ID", "IDPREV"),
                row.names = c(NA, 12L), class = "data.frame")

最终输出为:

#    ID IDPREV FINAL_ID
# 1   1     NA        1
# 2   2     NA        2
# 3   3     NA        3
# 4   4     NA        4
# 5   5     NA        5
# 6   6      1        1
# 7   7      2        2
# 8   8      3        3
# 9   9      4        4
# 10 10      5        5
# 11 11     10        5
# 12 12     11        5

【讨论】:

  • 我尝试使用igraph 复制您的答案。如果我遗漏了什么,请告诉我。
【解决方案2】:

您可以将数据对象合并到自身:

 dat <- structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, NA, NA, NA, NA, NA, 
 1, 2, 3, 4, 5), .Dim = c(10L, 2L), .Dimnames = list(NULL, c("ID", 
 "IDPREV")))
 dat2 <- merge(dat, dat, by.x=1, by.y=2)
Warning message:
In merge.data.frame(as.data.frame(x), as.data.frame(y), ...) :
  column name ‘ID’ is duplicated in the result

 dat2
#-----------------
  ID IDPREV ID
1  1     NA  6
2  2     NA  7
3  3     NA  8
4  4     NA  9
5  5     NA 10

您可能希望为第二个“ID”变量创建一个信息更丰富的列名。

 names(dat)[3] <- "Second.ID"

【讨论】:

    【解决方案3】:

    这也可以被概念化为图形对象并允许使用igraph 包。例如,使用@flodel 回答中的数据。

    df <- structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), 
                         IDPREV = c(NA, NA, NA, NA, NA, 1, 2, 3, 4, 5, 10, 11)),
                    .Names = c("ID", "IDPREV"),
                    row.names = c(NA, 12L), class = "data.frame")
    
    require(igraph)    
    # get rid of the effect of the NAs by matching these cases 
    # back to themselves; make a graph; then extract the clusters
    df$IDPREV[is.na(df$IDPREV)] <- df$ID[is.na(df$IDPREV)]
    g.el <- graph.data.frame(df)
    df$FINAL_ID_LM <- clusters(g.el)$membership
    

    给出与@flodel 的答案相同的结果:

    > df
       ID IDPREV FINAL_ID_LM FINAL_ID_FL
    1   1      1           1           1
    2   2      2           2           2
    3   3      3           3           3
    4   4      4           4           4
    5   5      5           5           5
    6   6      1           1           1
    7   7      2           2           2
    8   8      3           3           3
    9   9      4           4           4
    10 10      5           5           5
    11 11     10           5           5
    12 12     11           5           5
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      相关资源
      最近更新 更多