为什么apply(x,1,paste0(collapse =“"))在正值之间留有空白? [英] Why does apply(x, 1, paste0(collapse="") leave white space between positive values?
问题描述
在此示例中,当我跨列应用时,我得到的是空白的正数值而不是负数值?为什么是这样? paste0是否不应该删除元素之间的空格?这个问题背后的背景是,我正在尝试为googlemaps路线图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"))
我认为这与在具有不同数据类型(lat是字符而lon是数字)的数据帧上运行应用有关.
为什么显示空白?
paste0
不会添加空格-也不会删除空格.您可以通过在引导程序上调用paste0
进行测试.
apply
在矩阵和数组上运行,而不是在数据帧上运行.当您将数据帧传递给apply
时,它将被强制转换为矩阵.当然,关于矩阵的主要问题是所有元素都必须是同一类型.由于通常不能将字符串或因数强制转换为数字,因此将您的数字强制为字符串或因数以匹配第一列.如果检查as.matrix.data.frame
,您会看到format
用于此转换,并且?format
显示默认的trim = FALSE
trim
;如果FALSE
,则将逻辑,数字和复数值右对齐为相同的宽度:如果TRUE
,则禁止对齐的前导空白.
所以这是你的问题!
有什么解决方案?
paste
和paste0
是矢量化的,因此没有理由一次将apply
排成一行.您可以直接将各列粘贴在一起:
with(stop_latlon, paste0(lat, "%7", lon))
在确实需要apply
的更复杂的情况下,解决方案是处理您自己的矩阵转换,而不是依靠apply
进行默认设置.如果您将所有列字符串都设为之前,然后将数据传递给apply
(或者如果您使用字符矩阵而不是数据框),则转换将很简单(或不必要).>
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
; ifFALSE
, logical, numeric and complex values are right-justified to a common width: ifTRUE
the leading blanks for justification are suppressed.
So there's your problem!
What's the solution?
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).
这篇关于为什么apply(x,1,paste0(collapse =“"))在正值之间留有空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!