如何使用 R 按函数舍入特定列?
how to round specific columns by function using R?
我想对特定列进行四舍五入,每列都有不同的舍入值。我尝试使用以下代码,但出现错误:
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
错误是:
Error in [.data.table
(repo, , "namcol") :
column(s) not found: 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
高于给定代码的功能预期。帮我解决那个错误。我们将不胜感激。
改为这样做:
for (i in 1:length(col_rnm)) {
set(td, , col_rnm[i], round(td[, col_rnm[i], with = FALSE], col_rd[i]))
}
如果您查看 ?set
的帮助页面(与 ?":="
相同的帮助页面),您会看到它被描述为
set
is a low-overhead loop-able version of :=
您会发现 set
用于此处的许多答案,例如 and this one。
您的方法无效的原因:
- 您的循环中缺少
i
:roundCols(td,col_rnm,col_rd[i])
需要使用 col_rnm[i]
- 您的
roundCols
函数既不会使用 data.table
语法(set()
或 :=
)通过引用更新数据,也不会 return
更新后的数据数据,因此任何更改都是函数本地的
- 带引号的字符串
"namcol"
只是一个字符串。要使用参数 namcol
,您需要在不带引号的情况下使用它。
你不需要额外的功能---上面 set
的方法更简单。
我想对特定列进行四舍五入,每列都有不同的舍入值。我尝试使用以下代码,但出现错误:
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
错误是:
Error in
[.data.table
(repo, , "namcol") : column(s) not found: 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
高于给定代码的功能预期。帮我解决那个错误。我们将不胜感激。
改为这样做:
for (i in 1:length(col_rnm)) {
set(td, , col_rnm[i], round(td[, col_rnm[i], with = FALSE], col_rd[i]))
}
如果您查看 ?set
的帮助页面(与 ?":="
相同的帮助页面),您会看到它被描述为
set
is a low-overhead loop-able version of:=
您会发现 set
用于此处的许多答案,例如
您的方法无效的原因:
- 您的循环中缺少
i
:roundCols(td,col_rnm,col_rd[i])
需要使用col_rnm[i]
- 您的
roundCols
函数既不会使用data.table
语法(set()
或:=
)通过引用更新数据,也不会return
更新后的数据数据,因此任何更改都是函数本地的 - 带引号的字符串
"namcol"
只是一个字符串。要使用参数namcol
,您需要在不带引号的情况下使用它。
你不需要额外的功能---上面 set
的方法更简单。