Unwanted leading whitespace before number as char

Consider the following minimal working example:

df <- tibble::tibble(
	x=c(1, 10)
)

mwe <- function(row) {
	return(paste0(row[["x"]]))
}

df$mwe1 = apply(df, 1, mwe)
df$mwe2 = apply(df, 1, mwe)

The final value of df is

# A tibble: 2 × 3
      x mwe1  mwe2 
  <dbl> <chr> <chr>
1     1 1     " 1" 
2    10 10    "10" 

I expect it to be

# A tibble: 2 × 3
      x mwe1  mwe2 
  <dbl> <chr> <chr>
1     1 1     1 
2    10 10    10 

Can I get an explanation of why column mwe2 has the extra whitespace?

R.version info

platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          1.2                         
year           2021                        
month          11                          
day            01                          
svn rev        81115                       
language       R                           
version.string R version 4.1.2 (2021-11-01)
nickname       Bird Hippie

the lowest level technical reason is that.

format.default(c(1,10))
#  " 1" "10"
 format.default(c(1,100))
# "  1" "100"

i.e. default formatting a vector of numbers will pas the result character string to be as wide as the widest number seen.

format.default is called in your code because of your use of apply, that wants the input (df) to be a matrix.
try

(df <- tibble(
  x=c(1, 10),
  a=rep("a",2)))

as.matrix(df)

see how the matrix has coerced the numbers to char, due to the presence of the a column.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.