【问题标题】:data matching/data selection with multiple conditions in a long shaped database r长形数据库中具有多个条件的数据匹配/数据选择r
【发布时间】:2016-02-11 07:46:20
【问题描述】:

我一直在努力解决这个问题,这是一个相当复杂的数据选择,有多个可能的输出,我找不到表达式来得到我想要的。 我正在测量一群鸟类的离婚率。

可重现的数据库:

nest<- rep(seq(1:10),2)
year<- c(rep(2014, 10), rep(2015, 10))
pair<- c("TH4327_TH4317", "2", "TH8522_T75390" ,"4", "TJ1704_TJ1703", "TH4335_TH4333",
         "7", "8", "TH4337_TH4323", "T74703_TH1797",
         "TH4327_TH4317", "12", "TH8522_T75550","14", "TJ1704_NA" , "TH4335_TH4333",           "17", "TH8715_TH8714", "TH4388_TH4323", "TE9639_TH9675")
test<- data.frame(nest, year, pair)
test$pair <- as.character(test$pair)
test$year <- as.character(test$year)

下划线分隔一对中 2 个成员的 ID。如果没有 ID,则会放置一个不断增长的数字。显示每年相同的巢。 在连续 2 年中,我们有 5 种可能的情况(数字是巢 ID):

2014-2015 年同一对:1-6

2014-2015 空转:2-4-7

2014 年空但 2015 年被占用:8

在同一个巢中的变化对:10

一对成员之一的变化:3-9

未知:5

我追求的结果是:

在一起的对“SAME PAIR 2014-2015”:2
其中一个更改了“一对成员之一的更改”:2

我想出了如何计算保持在一起的对...

same<-test$pair[test$year=="2014"] %in% test$pair[test$year=="2015"]
table(same)

但是我无法获得关于离婚的情侣的信息。

我尝试了几个命令,which 和 ifelse,但都没有成功。

如果有不清楚的地方,我很乐意提供进一步的解释。我知道这是一个相当混乱的问题。

非常感谢, 一切顺利。

玩得开心

【问题讨论】:

    标签: r dataset selection


    【解决方案1】:

    这是一种使用合并的方法。策略如下。首先将这些对分成p1 和p2(我用tidyr::separate 做了这个)。然后我对跨年份的数据进行子集化,并使用p1 作为唯一标识符进行合并。这意味着现在将有两个不同的p2,一个是 2014 年的,一个是 2015 年的。现在可以直接测试团体是在一起还是离婚。

    如果你有很多年,这种方法需要推广。如果需要,我很乐意提供这样的概括。

    library(tidyr)
    
    test <- 
    test %>%
      filter(nchar(test$pair) > 3) %>% #getting rid of missing pairs
      separate(pair, c("p1", "p2"), "_") %>%
      select(-nest) #getting rid of nest which is superfluous 
    
    test <- merge(test[test$year=="2014",], test[test$year=="2015",], by = "p1", all = TRUE)
    
    #Same group across 2014 and 2015
    na.omit(test[test$p2.x == test$p2.y, grep("p", names(test))])
    
    #Different Group across 2014 and 2015
    na.omit(test[test$p2.x != test$p2.y, grep("p", names(test))])
    

    更新

    要概括多年的代码,请使用以下代码。这是比循环更好的方法。另请注意,上面的代码不起作用,因为我忘记包含 dplyr 库。请务必下载并加载dplyr 和tidyr。这些库非常适合数据操作。以下是tidyr 和dplyr 的一些来源。如果您还有其他问题,请告诉我。

    library(tidyr)
    library(dplyr)
    
    test <- 
    test %>%
      filter(nchar(test$pair) > 3) %>% #getting rid of missing pairs
      separate(pair, c("p1", "p2"), "_") %>% #splitting pairs
      select(-nest) #getting rid of nest which is superfluous 
    
    test <- split(test, test$year) #split data into lists by year
    test <- Map(function(d, n){names(d)[grepl("p2", names(d))] <- paste("p2", n, sep = "_"); d}, d = test, n = names(test)) #this line can be omitted.  It simply insures that your final data set looks nice.
    test <- Reduce(function(...) merge(..., by = "p1", all = TRUE), test)
    

    无包(即在 Base R 中)

    如果您不想使用 dplyr 和 tidyr 包,您可以用这种基本 R 方法替换前几行代码(直到调用 split)

    test <- test[nchar(test$pair) > 3, !names(test)%in%"nest"]
    
    split_pair <- do.call(rbind, strsplit(test$pair, "_"))
    
    test$p1 <- split_pair[, 1]
    test$p2 <- split_pair[, 2]
    test <- test[, !names(test)%in%"pair"]
    

    最终更新...希望

    玩得开心在下面的评论中提出了一个很好的观点。由于我使用p1 作为唯一标识符,因此无法识别p2 何时更改。为了克服这个问题,我做了以下...

     test <- split(test, test$year) #split data into lists by year
    
     test <- Reduce(function(...) merge(..., by = c("p1", "p2"), all = TRUE), test) #merge on both p1 and p2 to overcome the previous problem.  Pair are now unique identifiers
    
    #Stayed in same relationship
    stay = test$year.x == "2014" & test$year.y == "2015"
    na.omit(test[stay, ])
    
    #p1 changes couples between year.x and year.y
    tp1 <- test[test$p1 %in% test[duplicated(test$p1), "p1"], c("p1", "p2", "year.x", "year.y")]
    is_na <- (is.na(tp1$year.x) & is.na(tp1$year.y))
    stay_tp1 <- tp1$year.x == "2014" & tp1$year.y == "2015"
    stay_tp1[is.na(stay_tp1)] <- FALSE
    tp1 <- tp1[!(stay_tp1 | is_na), ]
    
    #A similar approach works for p2.  Notice it is probably best to do this in a function.  If you do use a function remember you will need to pass your variables as strings, unless you want to use NSE.
    

    最后一段代码可能有点混乱。让我解释。为了确定一只鸟是否改变了伙伴,我们识别重复,因为从一对移动到另一对的鸟会出现两次。然而,在多年的情况下,鸟类可以在几年中的任何一年改变对。要识别鸟改变的正确年份,您需要使用上面的代码。我建议你构造一个函数来处理这种情况,因为这涉及到相当多的打字。

    【讨论】:

    • 非常感谢雅各布的建议。我了解您的策略,但我不知道包裹。当我尝试运行脚本时,它给了我一个错误:no applicable method for 'separate_' applied to an object of class ...。是的......我确实有很多年了,我想我会通过它们循环或者你有另一个更好的策略来概括......
    • @havefun 我更新了。可能是代码不起作用,因为您还需要加载dplyr 库。
    • 我终于真正理解了你的脚本,但是你的解决方案没有处理一个复杂的问题,它并不总是第二个标识符发生变化,有一种情况,示例中的第九个,在这是第一个更改的标识符。未检测到这种情况(实际上我对具有 NA 的情况也不感兴趣)。您是否建议只运行两次相同的脚本,将 p2 作为唯一标识符,然后总结结果或有更好的解决方案?再次感谢,我从你的策略中学到了很多!
    • 非常感谢修复代码,现在问题解决了。但是我无法概括我的数据库的代码,因为太长(超过 5000 行)所以行 test &lt;- split(test, test$year) 创建一个“大列表”,当我运行 test &lt;- Reduce(function(...) merge(..., by = c("p1", "p2"), all = TRUE), test) 它给我一个错误“我无法分配大小为 2.0 gb 的向量"
    猜你喜欢
    • 2020-01-04
    • 2010-12-31
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多