【问题标题】:Turning multiple rows into a string by merging on ID in R通过合并R中的ID将多行变成一个字符串
【发布时间】:2021-08-02 21:00:51
【问题描述】:

Table1:(有数百个ID)

 participant_id hpo_term              year_of_birth  affected_relative   genome
    123         kidney failure          2000               Y                38
    123         hand tremor             2000               Y                38
    123         kidney transplant       2000               Y                38
    432         hypertension            1980               N                37
    432         exotropia               1980               N                37
    432         scissor gait            1980               N                37

我有两个查找表:(每个都有数百个值)

肾查:

kidney failure
kidney transplant
hypertension

非肾脏查询(每个都有数百个值):

hand tremor
exotropia
scissor gait

期望的结果:

participant_id kidney_hpo_term                   non_kidney_hpo_term    year_of_birth affected_relative   genome
123            kidney failure;kidney transplant  hand tremor            2000              Y                 38
432            hypertension                      exotropia;scissor gait 1980              Y                 37

最初我尝试过:

library(dplyr); library(tidyr)
pt.data %>% 
   mutate(kidney = hpo_term %in% kidney.hpo) %>%
   pivot_wider(names_from = kidney, values_from = hpo_term,
               values_fn = function(x)paste(x,collapse = ";"), values_fill = NA) %>%
   setNames(c("participant_id","Kidney","Non.kidney"))

kidney.hpo <- read.delim("kidney_hpo_terms.txt", header = F)

但我得到“values_fn[[value]] 中的错误;'closure' 类型的对象不是子集”

不确定我做错了什么,非常感谢您的帮助。

【问题讨论】:

    标签: r dplyr datatable subset lookup


    【解决方案1】:

    你的数据有几件事要说。

    首先,您的 table1 有重复的列:year_of_birthaffected_relativegenome 对于给定的参与者是相同的。

    最好将其存储在一个单独的表中,我将其命名为table1_short

    对于您的问题,只需检查一个术语是否在向量中,这是使用%in% 完成的。

    下面是你如何编写代码:

    library(tidyverse)
    table1=read.table(header=T, text="
    participant_id hpo_term              year_of_birth  affected_relative   genome
    123         'kidney failure'          2000               Y                38
    123         'hand tremor'             2000               Y                38
    123         'kidney transplant'       2000               Y                38
    432         hypertension              1980               N                37
    432         exotropia                 1980               N                37
    432         'scissor gait'            1980               N                37")
    
    table1_short = table1 %>% select(-hpo_term) %>% group_by(participant_id) %>% slice(1)
    table1_long = table1 %>% select(1:2)
    
    renal_lookup = c("kidney failure", "kidney transplant", "hypertension")
    nonrenal_lookup = c("hand tremor", "exotropia", "scissor gait")
    
    
    table1_long %>% 
      group_by(participant_id) %>% 
      summarise(
        kidney_hpo_term = hpo_term[hpo_term %in% renal_lookup] %>% paste(collapse=";"),
        non_kidney_hpo_term = hpo_term[hpo_term %in% nonrenal_lookup] %>% paste(collapse=";")
      ) %>% 
      left_join(table1_short, by="participant_id")
    #> # A tibble: 2 x 6
    #>   participant_id kidney_hpo_term                  non_kidney_hpo_term    year_of_birth affected_relative genome
    #>            <int> <chr>                            <chr>                          <int> <chr>              <int>
    #> 1            123 kidney failure;kidney transplant hand tremor                     2000 Y                     38
    #> 2            432 hypertension                     exotropia;scissor gait          1980 N                     37
    

    reprex package (v2.0.0) 于 2021-05-12 创建

    【讨论】:

    • 感谢您的回答。运行您的代码,我得到了决赛桌,但 hpo_term 列(肾和非肾为空白)。要读取查找表,我使用的是肾脏.hpo
    • @tacrolimus read.delim() 返回一个数据框,因此您可能想要选择第一列。抱歉,没有可重现的例子,我能做的也好不到哪里去。
    • 解决了,谢谢!抱歉无法共享代码。我在 HPC 的气闸中工作,很难将数据取出!你真的很有帮助,我非常感谢你花时间回答。是时候阅读 tidyverse 了!
    【解决方案2】:

    这可以通过data.table 中的dcast 来完成,如下所示:

    dtt[, group := paste0(
        ifelse(hpo_term %in% kidney_hpo, 'kidney', 'non_kidney'), '_hpo_term')]
    dcast(dtt, ... ~ group, value.var = 'hpo_term',
        fun.aggregate = paste, collapse = ';')
    #    participant_id year_of_birth affected_relative genome                  kidney_hpo_term
    # 1:            123          2000                 Y     38 kidney failure;kidney transplant
    # 2:            432          1980                 N     37                     hypertension
    #       non_kidney_hpo_term
    # 1:            hand tremor
    # 2: exotropia;scissor gait"
    

    【讨论】:

    • 感谢您花时间回答。当我尝试您的解决方案时,我会在“non_kidney_term”的末尾得到一个额外的列,其中包含所有术语,多个术语具有多个重复的 ID
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 2018-03-03
    • 2014-03-15
    • 1970-01-01
    相关资源
    最近更新 更多