替换有x行,数据有y行

时间:2019-03-20 14:11:56

标签: r rstudio

我一直在我的R代码中得到它:

Error in `$<-.data.frame`(`*tmp*`, isScanRequired, value = "No") : 
  replacement has 1 row, data has 0   

我不确定为什么。

> Compliance <- All %>% select(clientCode,
+                              MID,
+                              `Company Name`,
+                              `Most Recent SAQ Date`,
+                              `SAQ Version`,
+                              `SAQ Type`,
+                              `Compliance Program`,
+                              Assessor)
> 
> Compliance$qsa <- Compliance$Assessor
> 
> Compliance$isScanRequired <- "No"
Error in `$<-.data.frame`(`*tmp*`, isScanRequired, value = "No") : 
  replacement has 1 row, data has 0
> Compliance$isScanRequired[Compliance$`Compliance Program` == "SAQ_AND_SCAN"] <- "Yes"
> 
> 
> Compliance$saqType <- substring(Compliance$`SAQ Type`, regexpr("_", Compliance$`SAQ Type`) + 1, )
> 
> ComplianceClean <- Compliance %>% select(clientCode,
+                                          MID,
+                                          `Company Name`,
+                                          `Most Recent SAQ Date`,
+                                          `SAQ Version`,
+                                          saqType,
+                                          isScanRequired,
+                                          Assessor)
> 
> 
> colnames(ComplianceClean) <- c("clientCode",
+                                "account",
+                                "companyName",
+                                "effectiveDate",
+                                "pciDssVersion",
+                                "saqType",
+                                "isScanRequired",
+                                "qsa")
> 
> write.table(ComplianceClean, paste0("../", Sys.Date(), " Compliance Uplaod.txt"), sep = "\t", quote = FALSE, row.names = FALSE)
> 
> Scan <- All
> 
> Scan$isScanRequired <- "No"
**Error in `$<-.data.frame`(`*tmp*`, isScanRequired, value = "No") : 
  replacement has 1 row, data has 0**
> Scan$isScanRequired[Compliance$`Compliance Program` == "SAQ_AND_SCAN"] <- "Yes"
> 
> Scan <- Scan %>% filter(isScanRequired == "Yes" & `Scan Status` == "PASS")
> 
> #Scan$clientCode <- substring(Scan$`Sponsor Name`, regexpr("\\(", Scan$`Sponsor Name`) + 1, regexpr("\\)", Scan$`Sponsor Name`) - 1)
> 
> 
> ScanClean <- Scan %>% select(clientCode,
+                              MID,
+                              `Company Name`,
+                              `Most Recent Scan Date`,
+                              Assessor,
+                              `Scan Status`)
> 
> colnames(ScanClean) <- c("clientCode",
+                          "account",
+                          "companyName",
+                          "effectiveDate",
+                          "asv",
+                          "scanResult")  
> 

1 个答案:

答案 0 :(得分:0)

您的字符“否”对应1行,因此“替换项具有1行”。您必须指定要将数据分配给数据框的哪一行。

按行号分配:

Complience[1, "isScanRequired"] <- "No"

按条件分配:

Complience[Complience$some_column == "Yes", "isScanRequired"] <- "No"