R 函数中的列操作将数据帧转换为空值或意外值

R 函数中的列操作将数据帧转换为空值或意外值,r,R,我有一个运行良好的函数,我想通过删除其中一列并使用以下命令将另一列中的NA值转换为1来清理输出: df$PlateIden <- NULL df$PlateNum[is.na(df$PlateNum)] <- 1 df$platedenReplacedf$PlateNum[is.na(df$PlateNum)]您能与我们共享数据吗?添加的测试数据集您的测试数据是一个data.frame,只有一列?对吗?如果您想共享示例数据,最好使用dput。@AwesomeeExpres

我有一个运行良好的函数,我想通过删除其中一列并使用以下命令将另一列中的NA值转换为1来清理输出:

  df$PlateIden <- NULL

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

df$platedenReplace
df$PlateNum[is.na(df$PlateNum)]您能与我们共享数据吗?添加的测试数据集您的测试数据是一个
data.frame
,只有一列?对吗?如果您想共享示例数据,最好使用
dput
。@AwesomeeExpress您可以使用
df@Maurits Evers-是的,这是我的数据的一个片段。我的实际数据就像从目录中读取的40K行文件名,我将其作为数据帧读取,因为这样我就可以从那里展开。谢谢你的推荐,我不知道。谢谢你!希望我能理解原作不起作用,但是哦,好吧,不客气。谢谢你指出这一点。因为这很奇怪。我不知道为什么最后一行代码不能在函数内部工作。
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)
setDT(df)
  df[is.na(PlateNum), PlateNum := 1]. 
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