0
我正在为一个简单的ANOVA演示构建一个示例,如果没有将数字向量转换为“字符”向量,似乎无法设置小数长度,是否有解决方案?为什么format()将数字变量转换为字符变量?
# use fGarch for building somewhat messy data
library(fGarch)
N = 30
set.seed(1976)
control = rsnorm(N, 25, 5, 1.8)
treatOne = rnorm(N, 25, 4)
treatTwo = rsnorm(N, 22, 5, -1.3)
treatThree = rsnorm(N, 20, 5, -1)
# bind groups into a single dataframe
mysteryOne = data.frame(control, treatOne, treatTwo, treatThree)
# "stack" the dataframe to combine it into one column
mysteryOne = stack(mysteryOne)
# rename the columns
library(plyr)
mysteryOne = rename(mysteryOne, c("values" = "anxiety", "ind" = "condition"))
# replace the experimental "condition" values with a "group code"
mysteryOne$condition = c(rep(0, N), rep(1, N), rep(2, N), rep(3, N))
# specify vector types
mysteryOne[, 1] = as.numeric(mysteryOne[, 1])
mysteryOne[, 2] = as.factor(mysteryOne[, 2])
# restrict the numeric vector to two decimal points
mysteryOne[, 1] = format(round(mysteryOne[, 1], 2), nsmall = 2)
str(mysteryOne)
只是格式__after__做所有计算。 –
因为'format'函数的作用就是这样。请参阅帮助文件'?format'。如果要以更高的精度显示浮点数,请更改您的选项:'pi;选项(数字= 20); pi'。 – nrussell
谢谢,格式化后指定的类型是我现在要使用的默认方法。我想知道是否我可能不想使用format()命令。格式()帮助文件中的这个语句让我认为我错过了一些东西:“数字向量编码时所需的最小小数位数将所有元素显示为至少有效数字的位数。” –