r - numbers within apply get filled with spaces -
i want create folder name based on 2 variables in data frame , wrong here...
head(clustering) line x y qerror 1 200 15 19 1.118677 2 201 11 19 1.038482 3 202 0 6 1.238026 4 203 0 18 1.321878 5 204 0 12 1.204173 6 205 5 0 1.394773 str(clustering) 'data.frame': 3406 obs. of 4 variables: $ line : factor w/ 3406 levels "1001","1003",..: 814 822 831 841 847 858 891 ... $ x : num 15 11 0 0 0 5 16 8 1 0 ... $ y : num 19 19 6 18 12 0 14 19 17 18 ... $ qerror: num 1.12 1.04 1.24 1.32 1.2 ...
when try create folder name x , y columns, following:
apply(head(clustering),1,function(x){paste0('x',x[2],'_by_y',x[3])}) 1 2 3 4 5 6 "x15_by_y19" "x11_by_y19" "x 0_by_y 6" "x 0_by_y18" "x 0_by_y12" "x 5_by_y 0"
as can see, x , y additional space, if value smaller 10. when use without apply, seems work
paste0('x',clustering[3,2],'_by_y',clustering[3,3]) [1] "x0_by_y6"
has idea, why there spaces?
it's because of following lines apply
:
if (!dl) stop("dim(x) must have positive length") if (is.object(x)) x <- if (dl == 2l) as.matrix(x) else as.array(x)
as.matrix
calls format
, , format
+ as.matrix
can unexpected things. see "details" section as.matrix
. format
page notes "character strings padded blanks display width of widest."
here's demonstration showing format
responsible behavior have identified:
lapply(mydf, format) # $line # [1] "200" "201" "202" "203" "204" "205" # # $x # [1] "15" "11" " 0" " 0" " 0" " 5" # # $y # [1] "19" "19" " 6" "18" "12" " 0" # # $qerror # [1] "1.118677" "1.038482" "1.238026" "1.321878" "1.204173" "1.394773"
Comments
Post a Comment