【问题标题】:Replace a value and adding rows with if/for loop用 if/for 循环替换一个值并添加行
【发布时间】:2017-02-19 22:16:14
【问题描述】:

如果Dam 不是0,我想用新的Id 替换Sire。然后每次使用新的IdSex 添加一个新行。

例如,我需要将第一行中的 0 替换为s1073,并在数据中添加一个新行为1 s1073 0 0 2。同样,如果 Dam 为 0 且 Sir 不是 0,则在数据集中添加新行,例如在第 7 行,需要将 Dam 0 与 d900 重新组合,并在数据框中添加新行为 @ 987654334@.

谁能帮我解决这个问题?

FID  ID Sire  Dam  Sex
1 1832    0 1073   1
1 1833 1201 1251   2
1 1834   15  560   1
1 1835 1598 1583   1
1 1836    0   13   1
1 1837 1107  562   1
1 1838  900    0   1
1 1839  900  571   2
1 1840  900    0   1
1 1841    0  415   1
1 1842    0    0   2
1 1843 1201  303   2
1 1844    0    0   1
1 1845 1107  557   2
1 1846   15  749   2

【问题讨论】:

    标签: r bioinformatics genetics


    【解决方案1】:

    我猜这是plink FAM 格式,有些人缺少父亲或母亲,我们想为至少有一个父母的人添加缺少的父母,如果两者都缺失,则不要添加父母。

    # dummy fam data with missing parents
    df1 <- read.table(text = "FID   IID Father  Mother  Sex
    1   1   0   2   1
                      1 2   0   0   2
                      1 3   0   2   1
                      1 4   0   2   2
                      2 1   3   0   1
                      2 2   3   0   2
                      2 3   0   0   1
                      3 1   0   0   1
                      4 1   0   0   1
                      4 2   0   0   2
                      4 3   1   2   2
                      4 4   1   2   2
                      ", header = TRUE, 
                      colClasses = "character")
    

    注意,关于虚拟数据:
    - FID == 1 缺少父亲
    - FID == 2 缺少母亲
    - FID == 3 是一个没有父母的单身家庭
    - FID == 4 不缺父母

    任务,仅在其中一个缺失的情况下添加缺失的父亲或母亲。即:如果缺少父亲 == 0 和母亲 == 0,则不要添加父母。

    library(dplyr) # using dplyr for explicity of steps.
    
    # update 0 to IID for missing Father and Mother with suffix f and m
    df1 <- 
      df1 %>% 
      mutate(
        FatherNew = if_else(Father == "0" & Mother != "0", paste0(Mother, "f", IID), Father),
        MotherNew = if_else(Mother == "0" & Father != "0", paste0(Father, "m", IID), Mother))
    
    # add missing Fathers
    missingFather <- df1 %>% 
      filter(
        FatherNew != "0" &
          MotherNew != "0" &
          !FatherNew %in% df1$IID) %>% 
      transmute(
        FID = FID,
        IID = FatherNew,
        Father = "0",
        Mother = "0",
        Sex = "1") %>%
      unique
    
    
    # add missing Mothers
    missingMother <- df1 %>% 
      filter(
        FatherNew != "0" &
          MotherNew != "0" &
          !MotherNew %in% df1$IID) %>% 
      transmute(
        FID = FID,
        IID = MotherNew,
        Father = "0",
        Mother = "0",
        Sex = "2") %>%
      unique
    
    # update new Father/Mother IDs
    res <- df1 %>% 
      transmute(
        FID = FID,
        IID = IID,
        Father = FatherNew,
        Mother = MotherNew,
        Sex = Sex)
    
    # add missing Fathers/Mothers as new rows, and sort
    res <- rbind(
      res,
      missingFather,
      missingMother) %>%
      arrange(FID, IID)
    

    结果,检查输出

    res
    #    FID IID Father Mother Sex
    # 1    1   1    2f1      2   1
    # 2    1   2      0      0   2
    # 3    1 2f1      0      0   1
    # 4    1 2f3      0      0   1
    # 5    1 2f4      0      0   1
    # 6    1   3    2f3      2   1
    # 7    1   4    2f4      2   2
    # 8    2   1      3    3m1   1
    # 9    2   2      3    3m2   2
    # 10   2   3      0      0   1
    # 11   2 3m1      0      0   2
    # 12   2 3m2      0      0   2
    # 13   3   1      0      0   1
    # 14   4   1      0      0   1
    # 15   4   2      0      0   2
    # 16   4   3      1      2   2
    # 17   4   4      1      2   2
    

    【讨论】:

    • 太棒了。我90%的问题都解决了。最后一个问题 - 一些父亲或母亲超过 1 个,我想要一个单独的 id 给他们。例如在上面的例子中,父亲 '3' 来了两次,所以我需要母亲作为 3m1、3m2 ......并且在相同的时尚 IID 也应该调整......你知道我可以做到吗?
    • @user2808642 但是我们说兄弟姐妹来自同一个父亲 (3) 和两个不同的母亲 (3m1 和 3m2)?
    • @user2808642 如果您可以将我的虚拟数据复制到您的帖子中并添加预期的输出,那就太好了。
    • 谢谢,其实我这里不担心兄弟,这是绵羊谱系数据,需要运行其他模拟。您的虚拟数据的输出正是您在“res”中显示的那个。你能帮我把上面提到的3m1、3m2……加起来吗?
    • @user2808642 我认为这应该像粘贴IID 给新父母一样简单,更新帖子,看看这是不是你需要的。
    【解决方案2】:

    我认为这个答案对我找出失踪的后代非常有用。谢谢!

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-30
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    相关资源
    最近更新 更多