R - 回收先前非NA值的NA值

时间:2017-07-06 13:03:35

标签: r dplyr

我有一个名为input的数据框,如下所示:

structure(list(sequence = c("LdBPK_010012800.1", "MAQNDKIAPQDQDSF", 
"AQNDKIAPQDQDSFL", "QNDKIAPQDQDSFLD", "NDKIAPQDQDSFLDD", "DKIAPQDQDSFLDDQ", 
"KIAPQDQDSFLDDQP", "IAPQDQDSFLDDQPG", "APQDQDSFLDDQPGV", "PQDQDSFLDDQPGVR", 
"LdBPK_020009000.1", "MAQNDKIAPQDQDSF", "AQNDKIAPQDQDSFL", "QNDKIAPQDQDSFLD", 
"NDKIAPQDQDSFLDD", "DKIAPQDQDSFLDDQ", "KIAPQDQDSFLDDQP", "IAPQDQDSFLDDQPG", 
"APQDQDSFLDDQPGV", "PQDQDSFLDDQPGVR"), score = c(1, 17007, 12388, 
15984, 23405, 31897, 26826, 35239, 35361, 36486, 1, 17007, 12388, 
15984, 23405, 31897, 26826, 35239, 35361, 36486), epitope = structure(c(1L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L), .Label = c("", "Epitope", "Non-Epitope"), class = "factor"), 
    positioning = c(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE), accessions = c("LdBPK_010012800.1", 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, "LdBPK_020009000.1", 
    NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, -20L
), .Names = c("sequence", "score", "epitope", "positioning", 
"accessions"), class = "data.frame")

(实际上我的原始数据框有超过100万行,所以这只是它的一小部分)

我想回收input$accessions下的非NA值(从LdBPK_010012800.1开始),直到找到下一个非NA值(考虑到本示例,LdBPK_020009000.1)。然后我会将NA值回收到LdBPK_020009000.1以下,直到我遇到下一个非NA值,依此类推。

完成此操作后,我的新数据框应如下所示:

structure(list(sequence = c("LdBPK_010012800.1", "MAQNDKIAPQDQDSF", 
"AQNDKIAPQDQDSFL", "QNDKIAPQDQDSFLD", "NDKIAPQDQDSFLDD", "DKIAPQDQDSFLDDQ", 
"KIAPQDQDSFLDDQP", "IAPQDQDSFLDDQPG", "APQDQDSFLDDQPGV", "PQDQDSFLDDQPGVR", 
"LdBPK_020009000.1", "MAQNDKIAPQDQDSF", "AQNDKIAPQDQDSFL", "QNDKIAPQDQDSFLD", 
"NDKIAPQDQDSFLDD", "DKIAPQDQDSFLDDQ", "KIAPQDQDSFLDDQP", "IAPQDQDSFLDDQPG", 
"APQDQDSFLDDQPGV", "PQDQDSFLDDQPGVR"), score = c(1, 17007, 12388, 
15984, 23405, 31897, 26826, 35239, 35361, 36486, 1, 17007, 12388, 
15984, 23405, 31897, 26826, 35239, 35361, 36486), epitope = structure(c(1L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L), .Label = c("", "Epitope", "Non-Epitope"), class = "factor"), 
    positioning = c(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE), accessions = c("LdBPK_010012800.1", 
    "LdBPK_010012800.1", "LdBPK_010012800.1", "LdBPK_010012800.1", 
    "LdBPK_010012800.1", "LdBPK_010012800.1", "LdBPK_010012800.1", 
    "LdBPK_010012800.1", "LdBPK_010012800.1", "LdBPK_010012800.1", 
    "LdBPK_020009000.1", "LdBPK_020009000.1", "LdBPK_020009000.1", 
    "LdBPK_020009000.1", "LdBPK_020009000.1", "LdBPK_020009000.1", 
    "LdBPK_020009000.1", "LdBPK_020009000.1", "LdBPK_020009000.1", 
    "LdBPK_020009000.1")), row.names = c(NA, -20L), .Names = c("sequence", 
"score", "epitope", "positioning", "accessions"), class = "data.frame") 

我这样做是因为我的最终目标是使用dplyraccessions分组并获得score

下每组的总和

1 个答案:

答案 0 :(得分:0)

我们可以使用fill

library(tidyverse)
df1 %>% 
    fill(accessions)