不在write.csv或write.table本身.从帮助文档(?write.table):
几乎在所有情况下,数字量的转换由选项'"scipen"'(参见'options')控制,但内部等价于'digits = 15'.为了更好地控制,使用'format'来制作字符矩阵/数据帧,并在其上调用'write.table'.
您可能最好做的是使用自己格式化列format,ala:
set.seed(42)
df1 <- data.frame(x = runif(10), y = sample(10))
df1$x <- round(df1$x, digits = 2)
write.csv(df1, ...)
Run Code Online (Sandbox Code Playgroud)
编辑:
要在屏幕上进行漂亮的打印,只需options(digits = 2)再次使用和转储data.frame即可.我猜你想要漂亮地打印到CSV中,所以...
使用以下功能:
dfDigits <- function(x, digits = 2) {
## x is a data.frame
for (col in colnames(x)[sapply(x, class) == 'numeric'])
x[,col] <- round(x[,col], digits = digits)
x
}
set.seed(42)
df1 <- data.frame(x=runif(5),y=sample(5))
head(df1)
## x y
## 1 0.9148060435 3
## 2 0.9370754133 5
## 3 0.2861395348 1
## 4 0.8304476261 2
## 5 0.6417455189 4
head(dfDigits(df1, 2))
## x y
## 1 0.91 3
## 2 0.94 5
## 3 0.29 1
## 4 0.83 2
## 5 0.64 4
Run Code Online (Sandbox Code Playgroud)