【发布时间】:2019-04-17 11:56:31
【问题描述】:
我想对特定列进行四舍五入,每列具有不同的舍入值。我尝试使用以下代码,但它给出了错误:
roundCols <-function(repo, namcol, digiround){
repo[,"namcol"] = round(repo[,"namcol"], digits = digiround)
round.staus = TRUE
return(round.staus)
}
round.staus = FALSE
ils <- config[13]$ignoreColumns
ils <- gsub("\\{|\\}", "", ils)
ils <- ils %>% str_replace_all("\\&", ",")
coldrp <- unlist(strsplit(ils, "\\,"))
coldrp = gsub("[^a-zA-Z]+", ".", coldrp)
td <- fread(config[13]$save.location,stringsAsFactors = FALSE,drop=coldrp,blank.lines.skip = TRUE)
col_rnm <- c(names(td[,2]),names(td[,3])) #it has 2 column who's will be round off
col_rd <- c(2,3) #it gives digits how much rounding off required
for (i in 1:length(col_rnm)) {
round.staus = roundCols(td,col_rnm,col_rd[i])
}
td
错误是:
[.data.table(repo, , "namcol") 中的错误: 未找到列:namcol
我在控制台上尝试了相同的函数,它给出了准确的结果。
预期输出:
Account Chargeable.Capacity Expected.Capacity.in.30.days Deviation
Kishore 0.01 0.007 3.778268e-11
最初是我的数据:
Account Chargeable.Capacity Expected.Capacity.in.30.days Deviation
Kishore 0.007124108 0.007283185 3.778268e-11
高于给定代码的函数的预期值。帮我解决这个错误。努力将不胜感激。
【问题讨论】:
-
也许您可以提供
dput的数据,以便其他人可以重现您的问题? -
dput的数据在这里,structure(list(Account = "Kishore", Chargeable.Capacity = 0.01, Expected.Capacity.in.30.days = 0.0072831853027186, Deviation = 3.77826790758223e-11), row.names = c(NA, -1L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000000002571ef0>)。 -
@jay.sf 你说的对吧?
-
你想四舍五入到什么程度?您的
dput与您的结果相同。 -
我想对列的值进行四舍五入。我编辑了这个问题。我想现在你可以得到它了。
标签: r data.table rounding multiple-columns