简体   繁体   English

如何让 R 在读取 CSV 时插入“0”代替缺失值?

[英]How to make R insert a '0' in place of missing values while reading a CSV?

We have a multi-column CSV file of the following format:我们有以下格式的多列 CSV 文件:

id1,id2,id3,id4
1,2,3,4
,,3,4,6
2,,3,4

These missing values are to be assumed as a '0' when reading the CSV column by column.在逐列读取 CSV 时,这些缺失值将被假定为“0”。 The following is the script we currently have:以下是我们目前拥有的脚本:

data <- read.csv("data.csv")

dfList <- lapply(seq_along(data), function(i) {
    seasonal_per <- msts(data[, i], seasonal.periods=c(24,168))
    best_model <- tbats(seasonal_per)
    fcst <- forecast.tbats(best_model, h=24, level=90)
    dfForec <- print(fcst)
    result <- cbind(0:23, dfForec[, 1])
    result$id <- names(df)[i]

    return(result[c("id", "V1", "V2")])
})

finaldf <- do.call(rbind, dfList)
write.csv(finaldf, file = "out.csv", row.names = FALSE)

This script breaks when the CSV has missing values giving the error Error in tau + 1 + adj.beta + object$p: non-numeric argument to binary operator .当 CSV 缺少值并给出错误Error in tau + 1 + adj.beta + object$p: non-numeric argument to binary operator时,此脚本会中断。 How do we tell R to assume a '0' when it encounters a missing value?我们如何告诉 R 在遇到缺失值时假设为“0”?

I tried the following:我尝试了以下方法:

library("forecast")
D <- read.csv("data.csv",na.strings=".")
D[is.na(D)] <- 0

dfList <- lapply(seq_along(data), function(i) {
  seasonal_per <- msts(data[, i], seasonal.periods=c(24,168))
  best_model <- tbats(seasonal_per)
  fcst <- forecast.tbats(best_model, h=24, level=90)
  dfForec <- print(fcst)
  result <- cbind(0:23, dfForec[, 1])
  result$id <- names(df)[i]

  return(result[c("id", "V1", "V2")])
})

finaldf <- do.call(rbind, dfList)
write.csv(finaldf, file = "out.csv", row.names = FALSE)

but it gives the following error:但它给出了以下错误:

Error in data[, i]: object of type 'closure' is not subsettable

If you're certain that any NA value should be 0 , and that's the only issue, then 如果您确定任何 NA值都应该为0 ,那是唯一的问题,那么

data <- read.csv("data.csv")
data[is.na(data)] <- 0

If you're working in the tidyverse (or just with dplyr ), this option works well:如果您在tidyverse中工作(或仅使用dplyr ),则此选项效果很好:

library(tidyverse)
data <- read_csv("data.csv") %>% replace(is.na(.), 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM