cylondude - 1 year ago 190
R Question

# Why does apply(x, 1, paste0(collapse="") leave white space between positive values?

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.

``````library(dplyr)
stop_latlon <- data.frame(lat = paste0("via:", rnorm(10)), lon = rnorm(10))

stop_latlon %>%
apply(1, function(x) paste0(x, collapse = "%7"))
``````

edit: I think that it has something to do with running apply on a dataframe with different data types (lat is a character and lon is a numeric)

### Why does the white space show up?

`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

`trim`; if `FALSE`, logical, numeric and complex values are right-justified to a common width: if `TRUE` the leading blanks for justification are suppressed.

`paste` and `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).