Here is the sample code, which produces interesting output:
> gg<-data.frame(x=c("a","b"),y=as.integer(c(1000,100000)))
> gg
x y
1 a 1000
2 b 100000
> apply(gg,1,paste,collapse="")
[1] "a 1000" "b100000"
> apply(gg[1,],1,paste,collapse="")
1
"a1000"
In the first apply run, R somehow knows how to pad additional spaces. How does it do that and is it possible to control this behaviour?
applyonly works on an array or matrix, so it first has to convert your data.frame to a matrix.as.matrix(gg)creates the padding.Looking at
as.matrix.data.frame, the padding is caused by a call toformat(format.default, actually), which eventually callsprettyNum.prettyNumhas apreserve.widthargument with a default of"common".If you want more control over this behavior, convert each column in your data.frame to a character vector before calling
apply: