为什么字符列的类型是列表?

Why is the type of a character column a list?

为什么通过映射列表创建的列的类型?我希望它是一个字符列。如何将其转换为字符列?

t <- mtcars %>% mutate(new_col=map(mpg, function(x) as.character(x)))
typeof(t$new_col)

> [1] "list"

谢谢

您可以使用 map_chr() 而不是 map()

你可以只写

mtcars %>% mutate(new_col = map_chr(mpg, as.character))

map 的结果是一个列表。将列表添加到数据框通常并不明智,但可以做到。另一个常见的错误是将 POSIXlt 的结果添加到数据帧中。同样可以完成,但后续操作可能会失败。您可能刚刚使用了函数:

> t <- mtcars %>% mutate(new_col=as.character(mpg))
> typeof(t$new_col)
[1] "character"