When I apply across columns in this example I get a white space for positive valued numbers but not for negative values? Why is this? Shouldn't paste0 remove whitespace between elements? The context behind this problem is that I am trying to form endpoints for the googlemaps directions api.
stop_latlon <- data.frame(lat = paste0("via:", rnorm(10)), lon = rnorm(10))
apply(1, function(x) paste0(x, collapse = "%7"))
paste0 doesn't add white space - nor does it remove it. You can test this by just calling
paste0 on your vector.
apply runs on matrices and arrays, not data frames. When you pass a data frame to
apply, it is coerced to a matrix. The main thing about a matrix, of course, is that all elements must be the same type. Since strings or factors can't generally be coerced to numerics, your numeric is coerced to a string or factor to match the first column. If you examine
as.matrix.data.frame, you'll see that
format is used for this conversion, and
?format shows a default
trim = FALSE that says
FALSE, logical, numeric and complex values are right-justified to a common width: if
TRUEthe leading blanks for justification are suppressed.
So there's your problem!
paste0 are vectorized, so there's no reason to
apply them one row at a time. You can just paste the columns together directly:
with(stop_latlon, paste0(lat, "%7", lon))
In a more complicated case where
apply really would be necessary, the solution would be to handle your own matrix conversion rather than relying on
apply to do it with defaults. If you made all the columns strings before passing the data to
apply, (or if you used a character matrix instead of a data frame), the conversion would be straightforward (or unnecessary).