2017-12-18 87 views
1

我有一個運行良好的函數,我想通過刪除其中一列並使用這些命令將另一列中的NA值轉換爲1來清理輸出:函數內部的列操作將數據幀變爲空或意外的值

df$PlateIden <- NULL 

    df$PlateNum[is.na(df$PlateNum)] <- 1 

,當我直接使用我的功能之外的數據幀,但是當我在函數中使用他們我的數據框變成要麼是「空」或「1」,如果任一功能的值。這些工作用過的。

這裏是我的全部功能:

cleanup_safe <- function(df,addproject,adduser){ 

    colnames(df) <- "FileName" 

    df$RunDate <- str_match(df$FileName, "^[a-zA-Z ]*(\\d+)")[,2] 

    df$RunDate <- ymd(df$RunDate) 

    df$PlateNum <- str_match(df$FileName, "(?<=Plate|plate)[_ ]?(\\d)")[,2] 

    df$PlateIden <- str_match(df$FileName, "(?<=Plate|plate)[_ ]?\\d*[_ ]?([a-zA-Z])")[,2] 

    df$User <- "adduser" 

    df$Project <- "addproject" 

    df <- df[!duplicated(df[,c("User","Project","RunDate","PlateNum")]),] 

    df <- within(df, ID <- cumsum(!duplicated(df[c("User","Project","RunDate")]))) 

    df$PlateIden <- NULL 

    df$PlateNum[is.na(df$PlateNum)] <- 1 

} 

下面是測試數據集

test <- c("20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 1a, IgG-Biot, MAF.srbx", 
         "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 1a, IgG-Biot, SAF.srbx", 
         "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 1a, IgG-Biot.srbx", 
         "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 1b, IgG-Biot, MAF.srbx", 
         "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 1b, IgG-Biot, SAF.srbx", 
         "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 1b, IgG-Biot.srbx", 
         "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 2, IgG-Biot, MAF.srbx", 
         "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 2, IgG-Biot, SAF.srbx", 
         "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 2, IgG-Biot.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 1a, IgG-Biot, MAF.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 1a, IgG-Biot.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 1b, IgG-Biot, MAF.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 1b, IgG-Biot, SAF.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 1b, IgG-Biot.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 2a, IgG-Biot, MAF.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 2a, IgG-Biot, SAF.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 2a, IgG-Biot.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 2b, IgG-Biot, MAF.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 2b, IgG-Biot, SAF.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 2b, IgG-Biot.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 3a, IgG-Biot, MAF.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 3a, IgG-Biot, SAF.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 3a, IgG-Biot.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 3b, IgG-Biot, MAF.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 3b, IgG-Biot, SAF.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 3b, IgG-Biot.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 4a, IgG-Biot, MAF.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 4a, IgG-Biot, SAF.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 4a, IgG-Biot.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 4b, IgG-Biot, MAF.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 4b, IgG-Biot, SAF.srbx", 
         "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 4b, IgG-Biot.srbx", 
         "20160812, Optimization, Testing New lot of NGS, Plate 1, IgG-Biot, MAF.srbx", 
         "20160812, Optimization, Testing New lot of NGS, Plate 1, IgG-Biot, SAF.srbx", 
         "20160812, Optimization, Testing New lot of NGS, Plate 1, IgG-Biot.srbx", 
         "20160812, Optimization, Testing New lot of NGS, Plate 2, IgG-Biot, MAF.srbx", 
         "20160812, Optimization, Testing New lot of NGS, Plate 2, IgG-Biot, SAF.srbx", 
         "20160812, Optimization, Testing New lot of NGS, Plate 2, IgG-Biot.srbx", 
         "20160812_a, Optimization, Testing New lot of NGS, Plate 1, IgG-Biot, MAF.srbx", 
         "20160812_a, Optimization, Testing New lot of NGS, Plate 1, IgG-Biot, SAF.srbx", 
         "20160812_a, Optimization, Testing New lot of NGS, Plate 1, IgG-Biot.srbx", 
         "20160812_a, Optimization, Testing New lot of NGS, Plate 2, IgG-Biot, MAF.srbx", 
         "20160812_a, Optimization, Testing New lot of NGS, Plate 2, IgG-Biot, SAF.srbx", 
         "20160812_a, Optimization, Testing New lot of NGS, Plate 2, IgG-Biot.srbx") 

dataframe <- as.data.frame(test) 

dataframe <- cleanup_safe(dataframe,testproject,testuser) 
+0

你能與我們共享的數據? – suchait

+0

添加了測試數據集 – AwesomeeExpress

+0

您的測試數據是一個'data.frame'單個列嗎?那是對的嗎?如果你想分享樣本數據,最好使用'dput'。 –

回答

1

setDT(df) 
    df[is.na(PlateNum), PlateNum := 1]. 

所以更換df$PlateNum[is.na(df$PlateNum)] <- 1,你的整個會是這樣的:

使用data.table

library(data.table) 

test <- c("20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 1a, IgG-Biot, MAF.srbx", 
      "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 1a, IgG-Biot, SAF.srbx", 
      "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 1a, IgG-Biot.srbx", 
      "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 1b, IgG-Biot, MAF.srbx", 
      "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 1b, IgG-Biot, SAF.srbx", 
      "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 1b, IgG-Biot.srbx", 
      "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 2, IgG-Biot, MAF.srbx", 
      "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 2, IgG-Biot, SAF.srbx", 
      "20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 2, IgG-Biot.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 1a, IgG-Biot, MAF.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 1a, IgG-Biot.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 1b, IgG-Biot, MAF.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 1b, IgG-Biot, SAF.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 1b, IgG-Biot.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 2a, IgG-Biot, MAF.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 2a, IgG-Biot, SAF.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 2a, IgG-Biot.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 2b, IgG-Biot, MAF.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 2b, IgG-Biot, SAF.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 2b, IgG-Biot.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 3a, IgG-Biot, MAF.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 3a, IgG-Biot, SAF.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 3a, IgG-Biot.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 3b, IgG-Biot, MAF.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 3b, IgG-Biot, SAF.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 3b, IgG-Biot.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 4a, IgG-Biot, MAF.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 4a, IgG-Biot, SAF.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 4a, IgG-Biot.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 4b, IgG-Biot, MAF.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 4b, IgG-Biot, SAF.srbx", 
      "20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 4b, IgG-Biot.srbx", 
      "20160812, Optimization, Testing New lot of NGS, Plate 1, IgG-Biot, MAF.srbx", 
      "20160812, Optimization, Testing New lot of NGS, Plate 1, IgG-Biot, SAF.srbx", 
      "20160812, Optimization, Testing New lot of NGS, Plate 1, IgG-Biot.srbx", 
      "20160812, Optimization, Testing New lot of NGS, Plate 2, IgG-Biot, MAF.srbx", 
      "20160812, Optimization, Testing New lot of NGS, Plate 2, IgG-Biot, SAF.srbx", 
      "20160812, Optimization, Testing New lot of NGS, Plate 2, IgG-Biot.srbx", 
      "20160812_a, Optimization, Testing New lot of NGS, Plate 1, IgG-Biot, MAF.srbx", 
      "20160812_a, Optimization, Testing New lot of NGS, Plate 1, IgG-Biot, SAF.srbx", 
      "20160812_a, Optimization, Testing New lot of NGS, Plate 1, IgG-Biot.srbx", 
      "20160812_a, Optimization, Testing New lot of NGS, Plate 2, IgG-Biot, MAF.srbx", 
      "20160812_a, Optimization, Testing New lot of NGS, Plate 2, IgG-Biot, SAF.srbx", 
      "20160812_a, Optimization, Testing New lot of NGS, Plate 2, IgG-Biot.srbx") 

dataframe <- as.data.frame(test) 


    cleanup_safe <- function(df,addproject,adduser){ 

    colnames(df) <- "FileName" 

    df$RunDate <- str_match(df$FileName, "^[a-zA-Z ]*(\\d+)")[,2] 

    df$RunDate <- ymd(df$RunDate) 

    df$PlateNum <- str_match(df$FileName, "(?<=Plate|plate)[_ ]?(\\d)")[,2] 

    df$PlateIden <- str_match(df$FileName, "(?<=Plate|plate)[_ ]?\\d*[_ ]?([a-zA-Z])")[,2] 

    df$User <- "adduser" 

    df$Project <- "addproject" 

    df <- df[!duplicated(df[,c("User","Project","RunDate","PlateNum")]),] 

    df <- within(df, ID <- cumsum(!duplicated(df[c("User","Project","RunDate")]))) 

    df <- df[, !(names(df) %in% "PlateIden"), drop = F] 

    setDT(df) 
    df[is.na(PlateNum), PlateNum := 1] 



} 

dataframe <- cleanup_safe(dataframe,testproject,testuser) 

這會給你的輸出: -

FileName RunDate PlateNum User Project ID 
1: 20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 1a, IgG-Biot, MAF.srbx 2016-08-01  1 adduser addproject 1 
2: 20160801, Optimization, gp70_B.CaseA_V1_V2 Coupling Testing, Plate 2, IgG-Biot, MAF.srbx 2016-08-01  2 adduser addproject 1 
3: 20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 1a, IgG-Biot, MAF.srbx 2016-08-02  1 adduser addproject 2 
4: 20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 2a, IgG-Biot, MAF.srbx 2016-08-02  2 adduser addproject 2 
5: 20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 3a, IgG-Biot, MAF.srbx 2016-08-02  3 adduser addproject 2 
6: 20160802, Optimization, New lot of gp70_B.CaseA_V1_V2 Testing, Plate 4a, IgG-Biot, MAF.srbx 2016-08-02  4 adduser addproject 2 
7:     20160812, Optimization, Testing New lot of NGS, Plate 1, IgG-Biot, MAF.srbx 2016-08-12  1 adduser addproject 3 
8:     20160812, Optimization, Testing New lot of NGS, Plate 2, IgG-Biot, MAF.srbx 2016-08-12  2 adduser addproject 3 
+1

謝謝Suchait!希望我理解,而原來不工作,但哦。 – AwesomeeExpress

+0

不客氣。感謝您指出了這一點。因爲它很奇怪。我不知道爲什麼最後一行代碼不在函數內部工作。 – suchait

相關問題