簡體   English   中英

如何讓 R 在讀取 CSV 時插入“0”代替缺失值?

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

我們有以下格式的多列 CSV 文件:

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

在逐列讀取 CSV 時,這些缺失值將被假定為“0”。 以下是我們目前擁有的腳本:

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)

當 CSV 缺少值並給出錯誤Error in tau + 1 + adj.beta + object$p: non-numeric argument to binary operator時,此腳本會中斷。 我們如何告訴 R 在遇到缺失值時假設為“0”?

我嘗試了以下方法:

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)

但它給出了以下錯誤:

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

如果您確定任何 NA值都應該為0 ,那是唯一的問題,那么

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

如果您在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