2017-07-06 80 views
2

我有一個數據幀稱爲input,看起來像下面這樣:的R - 回收NA值以前的非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", 
    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)。然後我將回收低於LdBPK_020009000.1的NA值,直到遇到下一個非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") 

我這樣做,因爲我的最終目標是通過accessions使用dplyr分組和score

下獲得各組的總和

回答

0

我們可以用fill

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