生成示例数据集的代码如下所示:
z = data.frame(a = c(1:4),b = as.character(c(1:4)),c = c('s,','d','g','y'))
zz = data.frame(a = c(5:8),b = as.numeric(c(5:8)),e = c(2,4,5,6))
zzz = data.frame(a = c(9:12),b = as.numeric(c(9:12)),r = c(4,8,6,5))
z4 = list(z,zz,zzz)
lapply(z4,function(x)x %>% select(a,b)) %>% bind_rows
输出为
> lapply(z4,function(x)x %>% select(a,b)) %>% bind_rows
Error in `bind_rows()`:
! Can't combine `..1$b` <character> and `..2$b` <double>.
Run `rlang::last_error()` to see where the error occurred.
是否可以同时使用select和as. numeric?
1条答案
按热度按时间rxztt3cl1#
我们可以使用
type.convert
自动将列表元素转换为适当的类型这可能并不适用于所有条件,例如,如果存在包含非数值元素的列,则该列将保持为
character
。在这种情况下,不将列转换为numeric
(对于非数值,这可能返回NA),而是将所有列转换为character
,绑定它们,然后应用type.convert