如何用NA来pivot_wider多个成对变量?

68bkxrlz  于 2023-04-03  发布在  其他
关注(0)|答案(2)|浏览(108)

我只是有一个截断和subseted df与一个成对的一组3个变量,每个看起来像这样;

structure(list(UID = c("20210521-3", "20210521-1", "20210521-4", 
"20210526-59", "20210526-11", "20210526-62", "20210526-12", "20210526-46", 
"20210526-13", "20210526-44", "20210526-33", "20210526-14", "20210526-9", 
"20210526-10", "20210526-52", "20210526-56", "20210526-43", "20210526-32", 
"20210526-4", "20210526-41", "20210526-48", "20210526-65", "20210526-58", 
"20210526-40", "20210526-5", "20210526-38", "20210526-55", "20210526-60", 
"20210526-61", "20210526-2", "20210526-1", "20210526-63", "20210526-37", 
"20210526-36", "20210526-35", "20210526-8", "20210526-19", "20210526-18", 
"20210526-30", "20210526-27", "20210526-26", "20210526-24", "20210526-25", 
"20210526-22", "20210526-47", "20210526-31", "20210526-39", "20210526-54", 
"20210526-64", "20210526-53", "20210526-21", "20210526-42", "20210526-20", 
"20210526-57", "20210526-50", "20210526-15", "20210526-3", "20210526-23", 
"20210526-45", "20210526-6", "20210526-34", "20210526-7", "20210526-17", 
"20210526-29", "20210526-28", "20210526-51", "20210526-49", "20210536-2", 
"20210536-1", "20210521-NA", "20210521-NA", "20210521-NA", "20210521-NA", 
"20210521-NA", "20210521-NA", "20210526-NA", "20210526-NA", "20210526-NA", 
"20210526-NA", "20210526-NA", "20210526-NA", "20210526-NA", "20210526-NA", 
"20210526-NA", "20210526-NA", "20210526-NA", "20210526-NA", "20210526-NA", 
"20210526-NA", "20210526-NA", "20210526-NA", "20210526-NA", "20210526-NA", 
"20210526-NA", "20210536-NA", "20210536-NA", "20210536-NA", "20210536-NA", 
"20210536-NA", "20210536-NA", "20210536-NA", "20210536-NA", "20210536-NA", 
"20210536-NA", "20210536-NA", "20210536-NA", "20210536-NA", "20210536-NA", 
"20210536-NA", "20210536-NA", "20210536-NA", "20210536-NA", "20210536-NA", 
"20210536-NA", "20210536-NA", "20210536-NA", "20210521-3", "20210521-1", 
"20210521-4", "20210526-59", "20210526-11", "20210526-62", "20210526-12", 
"20210526-46", "20210526-13", "20210526-44", "20210526-33", "20210526-14", 
"20210526-9", "20210526-10", "20210526-52", "20210526-56", "20210526-43", 
"20210526-32", "20210526-4", "20210526-41", "20210526-48", "20210526-65", 
"20210526-58", "20210526-40", "20210526-5", "20210526-38", "20210526-55", 
"20210526-60", "20210526-61", "20210526-2", "20210526-1", "20210526-63", 
"20210526-37", "20210526-36", "20210526-35", "20210526-8", "20210526-19", 
"20210526-18", "20210526-30", "20210526-27", "20210526-26", "20210526-24", 
"20210526-25", "20210526-22", "20210526-47", "20210526-31", "20210526-39", 
"20210526-54", "20210526-64", "20210526-53", "20210526-21", "20210526-42", 
"20210526-20", "20210526-57", "20210526-50", "20210526-15", "20210526-3", 
"20210526-23", "20210526-45", "20210526-6", "20210526-34", "20210526-7", 
"20210526-17", "20210526-29", "20210526-28", "20210526-51", "20210526-49", 
"20210536-2", "20210536-1", "20210521-NA", "20210521-NA", "20210521-NA", 
"20210521-NA", "20210521-NA", "20210521-NA", "20210526-NA", "20210526-NA", 
"20210526-NA", "20210526-NA", "20210526-NA", "20210526-NA", "20210526-NA", 
"20210526-NA", "20210526-NA", "20210526-NA", "20210526-NA", "20210526-NA", 
"20210526-NA", "20210526-NA", "20210526-NA", "20210526-NA", "20210526-NA", 
"20210526-NA", "20210526-NA", "20210536-NA", "20210536-NA", "20210536-NA", 
"20210536-NA", "20210536-NA", "20210536-NA", "20210536-NA", "20210536-NA", 
"20210536-NA", "20210536-NA", "20210536-NA", "20210536-NA", "20210536-NA", 
"20210536-NA", "20210536-NA", "20210536-NA", "20210536-NA", "20210536-NA", 
"20210536-NA", "20210536-NA", "20210536-NA", "20210536-NA"), 
    Cross = c("NTBG X SDZ", "NTBG X SDZ", "NTBG X SDZ", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "CBG X CBG", "CBG X CBG", 
    "NTBG X SDZ", "NTBG X SDZ", "NTBG X SDZ", "NTBG X SDZ", "NTBG X SDZ", 
    "NTBG X SDZ", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "CBG X CBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", 
    "CBG X CBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", 
    "CBG X CBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", 
    "CBG X CBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", 
    "CBG X CBG", "CBG X CBG", "NTBG X SDZ", "NTBG X SDZ", "NTBG X SDZ", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "CBG X CBG", 
    "CBG X CBG", "NTBG X SDZ", "NTBG X SDZ", "NTBG X SDZ", "NTBG X SDZ", 
    "NTBG X SDZ", "NTBG X SDZ", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", "SDZ X UCBG", 
    "SDZ X UCBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", 
    "CBG X CBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", 
    "CBG X CBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", 
    "CBG X CBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", "CBG X CBG", 
    "CBG X CBG", "CBG X CBG", "CBG X CBG"), Rep = c("A", "B", 
    "C", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
    "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
    "B", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "C", 
    "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
    "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", 
    "D", "D", "D", "D", "D", "A", "B", "A", "B", "B", "C", "D", 
    "D", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", 
    "B", "B", "C", "C", "C", "D", "D", "D", "A", "A", "A", "A", 
    "A", "A", "B", "C", "C", "C", "C", "C", "D", "D", "D", "D", 
    "D", "D", "D", "D", "D", "D", "A", "B", "C", "A", "A", "A", 
    "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", 
    "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
    "B", "B", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
    "C", "C", "C", "C", "C", "C", "C", "C", "D", "D", "D", "D", 
    "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", 
    "D", "A", "B", "A", "B", "B", "C", "D", "D", "A", "A", "A", 
    "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "C", "C", 
    "C", "D", "D", "D", "A", "A", "A", "A", "A", "A", "B", "C", 
    "C", "C", "C", "C", "D", "D", "D", "D", "D", "D", "D", "D", 
    "D", "D"), Garden = c("Limahuli", NA, NA, NA, NA, "Limahuli", 
    "Limahuli", "Limahuli", NA, NA, NA, "Limahuli", NA, "Limahuli", 
    NA, "Limahuli", "Limahuli", NA, NA, "Limahuli", NA, NA, NA, 
    NA, "Limahuli", "Limahuli", "Limahuli", "Limahuli", NA, "Limahuli", 
    NA, "Limahuli", "Limahuli", NA, "Limahuli", "Limahuli", "Limahuli", 
    NA, "Limahuli", NA, "Limahuli", "Limahuli", NA, NA, "Limahuli", 
    NA, NA, "Limahuli", NA, NA, NA, "Limahuli", "Limahuli", NA, 
    NA, "Limahuli", NA, "Limahuli", NA, "Limahuli", NA, NA, NA, 
    "Limahuli", NA, "Limahuli", "Limahuli", NA, "Limahuli", NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, "McBryde", "McBryde", "McBryde", "McBryde", NA, NA, 
    NA, "McBryde", "McBryde", "McBryde", NA, "McBryde", NA, "McBryde", 
    NA, NA, "McBryde", "McBryde", NA, "McBryde", "McBryde", "McBryde", 
    "McBryde", NA, NA, NA, NA, "McBryde", NA, "McBryde", NA, 
    NA, "McBryde", NA, NA, NA, "McBryde", NA, "McBryde", NA, 
    NA, "McBryde", "McBryde", NA, "McBryde", "McBryde", NA, NA, 
    "McBryde", "McBryde", NA, NA, "McBryde", "McBryde", NA, "McBryde", 
    NA, "McBryde", NA, "McBryde", "McBryde", "McBryde", NA, "McBryde", 
    NA, NA, "McBryde", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA), Days1 = c(17, 17, 17, 17, 
    17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
    17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
    17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
    17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
    17, 17, 17, 17, 17, 16, 17, 16, 17, 17, 17, 17, 17, 17, 17, 
    16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
    17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
    17, 17, 17, 17, 17, 17, 16, 17, 17, 17, 17, 17, 17, 17, 17, 
    17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
    17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
    17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
    17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
    17, 16, 17, 16, 17, 17, 17, 17, 17, 17, 17, 16, 17, 17, 17, 
    17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
    17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
    17, 17, 16), Days2 = c(59, 59, 59, 59, 59, 59, 59, 59, 59, 
    59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 
    59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 
    59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 
    59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 
    59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 
    59, 59, 59, 59, 59, 60, 60, 59, 59, 59, 59, 59, 59, 59, 59, 
    59, 59, 59, 59, 59, 59, 60, 59, 59, 59, 59, 59, 59, 59, 60, 
    60, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 
    59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 
    59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 
    59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 
    59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 
    59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 
    59, 60, 60, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 
    59, 59, 60, 59, 59, 59, 59, 59, 59, 59, 60, 60, 59), Days3 = c(72, 
    79, 79, 79, 76, 79, 76, 79, 76, 79, 79, 76, 76, 76, 72, 72, 
    72, 79, 76, 79, 76, 79, 76, 76, 76, 76, 76, 76, 76, 76, 76, 
    72, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 72, 72, 
    72, 72, 72, 72, 76, 79, 76, 79, 79, 76, 79, 76, 79, 76, 76, 
    76, 76, 76, 76, 72, 72, 76, 72, 65, 79, 65, 76, 79, 79, 76, 
    76, 72, 72, 65, 79, 79, 76, 76, 76, 76, 76, 76, 72, 69, 62, 
    79, 76, 76, 76, 76, 76, 72, 72, 72, 72, 76, 72, 72, 72, 69, 
    76, 72, 72, 76, 76, 76, 76, 69, 69, 65, 72, 79, 79, 79, 76, 
    79, 76, 79, 76, 79, 79, 76, 76, 76, 72, 72, 72, 79, 76, 79, 
    76, 79, 76, 76, 76, 76, 76, 76, 76, 76, 76, 72, 76, 76, 76, 
    76, 76, 76, 76, 76, 76, 76, 76, 76, 72, 72, 72, 72, 72, 72, 
    76, 79, 76, 79, 79, 76, 79, 76, 79, 76, 76, 76, 76, 76, 76, 
    72, 72, 76, 72, 65, 79, 65, 76, 79, 79, 76, 76, 72, 72, 65, 
    79, 79, 76, 76, 76, 76, 76, 76, 72, 69, 62, 79, 76, 76, 76, 
    76, 76, 72, 72, 72, 72, 76, 72, 72, 72, 69, 76, 72, 72, 76, 
    76, 76, 76, 69, 69, 65), Check1.Survival = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L), levels = c("alive", "dead"), class = "factor"), 
    Check2.Survival = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, NA, 2L, NA, NA, NA, NA, 1L, NA, 2L, NA, 2L, NA, 2L, 
    2L, NA, NA, 2L, 2L, NA, 2L, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 2L, 
    NA, NA, NA, NA, 1L, NA, 2L, NA, 2L, NA, 2L, 2L, NA, NA, 2L, 
    2L, NA, 2L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA
    ), levels = c("alive", "dead"), class = "factor"), Check3.Survival = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, NA, NA, NA, NA, NA, 2L, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, NA, NA, NA, NA, NA, NA, 2L, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA), levels = c("alive", "dead"), class = "factor")), row.names = c(NA, 
-232L), class = c("tbl_df", "tbl", "data.frame"))

“DaysX”前缀与“CheckX”前缀配对。我只需要一个长的df(或tibble)与这些变量配对。
我试过了

test<-out %>%
  pivot_longer(
    cols = c(Days1:Days10, Check1.Survival:Check10.Survival),
    names_to = c(".value", "rating"),
    names_pattern = "([a-z]+)_*(.+)") %>%
  rename(Days = ays, Survival = heck)

但变量不是成对的。我也试过了,

test2<-out%>%
  pivot_longer(
    c(-UID, -Cross, -Rep, -Garden),
    names_to = c(".value", "item"), 
    names_sep = "_"
  ) %>% 
  select(-item)

但这似乎根本不能改变df。我知道我错过了一些超级简单的东西。

5tmbdcev

5tmbdcev1#

下面是一个Liner:

library(tidyr)
pivot_longer(df, cols=tidyr::matches(".+\\d.*"), 
              names_pattern = "(\\D+)(\\d)", names_to = c(".value", "num"))

# A tibble: 696 × 7
  UID         Cross      Rep   Garden   num    Days Check
  <chr>       <chr>      <chr> <chr>    <chr> <dbl> <fct>
1 20210521-3  NTBG X SDZ A     Limahuli 1        17 alive
2 20210521-3  NTBG X SDZ A     Limahuli 2        59 alive
3 20210521-3  NTBG X SDZ A     Limahuli 3        72 alive
4 20210521-1  NTBG X SDZ B     NA       1        17 alive
5 20210521-1  NTBG X SDZ B     NA       2        59 alive
6 20210521-1  NTBG X SDZ B     NA       3        79 alive
7 20210521-4  NTBG X SDZ C     NA       1        17 alive
8 20210521-4  NTBG X SDZ C     NA       2        59 alive
9 20210521-4  NTBG X SDZ C     NA       3        79 alive
10 20210526-59 SDZ X UCBG A     NA       1        17 alive

解析正则表达式:

  • matches(“.+\d.”)-以多个字符(.+)开头,后跟数字(\d),以0或多个字符(.)结尾
  • (\D+)(\d)-分为2部分,第一部分至少有1个非数字“(\D+)”,第二部分为单个数字“(\d)”
  • names_to = c(“.value”,“num”)-从前面的name_pattern中取出两部分,使用第一部分创建列名'. value'。然后取出第二部分并将其放置在名为的列中:“数字”
sycxhyv7

sycxhyv72#

我无法想象你想要的结果这是您的示例数据的预期结果吗?

df %>%
  pivot_longer(starts_with("Check"),
               names_pattern = "(.*\\d)\\.(.*)",
               names_to = c("Check",".value"),
               names_prefix = "Check") %>%
  pivot_longer(starts_with("Days"),
               names_to = "Day",
               names_prefix = "Days") %>%
  filter(Check == Day)
#> # A tibble: 696 × 8
#>    UID         Cross      Rep   Garden   Check Survival Day   value
#>    <chr>       <chr>      <chr> <chr>    <chr> <fct>    <chr> <dbl>
#>  1 20210521-3  NTBG X SDZ A     Limahuli 1     alive    1        17
#>  2 20210521-3  NTBG X SDZ A     Limahuli 2     alive    2        59
#>  3 20210521-3  NTBG X SDZ A     Limahuli 3     alive    3        72
#>  4 20210521-1  NTBG X SDZ B     <NA>     1     alive    1        17
#>  5 20210521-1  NTBG X SDZ B     <NA>     2     alive    2        59
#>  6 20210521-1  NTBG X SDZ B     <NA>     3     alive    3        79
#>  7 20210521-4  NTBG X SDZ C     <NA>     1     alive    1        17
#>  8 20210521-4  NTBG X SDZ C     <NA>     2     alive    2        59
#>  9 20210521-4  NTBG X SDZ C     <NA>     3     alive    3        79
#> 10 20210526-59 SDZ X UCBG A     <NA>     1     alive    1        17
#> # … with 686 more rows

创建于2023-03-31带有reprex v2.0.2

相关问题