0
在本例中我使用了两个数据帧I rbind
,结果是所有值都有两个十进制数。不过,我想有1行中没有十进制数和2使用不同小数的值对数据帧进行rbind
one <- data.frame(matrix(c(1,2,3,4), ncol=2)); one
two <- data.frame(matrix(c(1.63,8.88,9.17,2.31), ncol=2)); two
rbind(one, two)
在本例中我使用了两个数据帧I rbind
,结果是所有值都有两个十进制数。不过,我想有1行中没有十进制数和2使用不同小数的值对数据帧进行rbind
one <- data.frame(matrix(c(1,2,3,4), ncol=2)); one
two <- data.frame(matrix(c(1.63,8.88,9.17,2.31), ncol=2)); two
rbind(one, two)
您可以使用sprintf
但这将您的变量转换成字符(而不是数字),即
d1 <- rbind(one, two)
d1[] <- lapply(d1, function(i) sprintf('%.6g', i))
其给出,
X1 X2 1 1 3 2 2 4 3 1.63 9.17 4 8.88 2.31
但是结构,
str(d1)
'data.frame': 4 obs. of 2 variables:
$ X1: chr "1" "2" "1.63" "8.88"
$ X2: chr "3" "4" "9.17" "2.31"
如果你将其转换为数字,你会再次得到这些小数,即
sapply(d1, as.numeric)
# X1 X2
#[1,] 1.00 3.00
#[2,] 2.00 4.00
#[3,] 1.63 9.17
#[4,] 8.88 2.31
也许是这样的:
test<-formatC(as.matrix(two),digits=2,format="f")
rbind(one,test)
您可能需要在'list'存储否则由于类型提升,将会有一个类,即“list(one,two)” – akrun