我有以下几点:
data_frame(type = list( c('1','2', 'text'), c(1L ,2L), c(1.5, 2.1), c(TRUE, FALSE))) %>%
mutate(typeof=unlist(map(type, typeof)),
mode= unlist(map(type, mode)),
class= unlist(map(type, class)))
# A tibble: 4 x 4
type typeof mode class
<list> <chr> <chr> <chr>
1 <chr [3]> character character character
2 <int [2]> integer numeric integer
3 <dbl [2]> double numeric numeric
4 <lgl [2]> logical logical logical
我想添加一个包含列类型内容的列,如:
# A tibble: 4 x 4
type typeof mode class vector
<list> <chr> <chr> <chr> <chr>
1 <chr [3]> character character character c('1','2', 'text')
2 <int [2]> integer numeric integer c(1L ,2L)
3 <dbl [2]> double numeric numeric c(1.5, 2.1)
4 <lgl [2]> logical logical logical c(TRUE, FALSE)
我尝试unlist(map(type, quote))
,但它给出了:
# A tibble: 4 x 5
type typeof mode class vector
<list> <chr> <chr> <chr> <list>
1 <chr [3]> character character character <symbol>
2 <int [2]> integer numeric integer <symbol>
3 <dbl [2]> double numeric numeric <symbol>
4 <lgl [2]> logical logical logical <symbol>
甚至不确定<symbol>
是什么......
答案 0 :(得分:0)
首先,如果您正在使用purrr
包,则在创建示例数据框时可能不需要unlist
。我们可以使用map_chr
来获得相同的输出。
library(tidyverse)
dt <- data_frame(type = list(c('1','2', 'text'), c(1L ,2L), c(1.5, 2.1), c(TRUE, FALSE))) %>%
mutate(typeof = map_chr(type, typeof),
mode = map_chr(type, mode),
class = map_chr(type, class))
至于您想要的输出,我认为我们可以使用map_chr
和toString
来创建包含列表中所有内容的字符串。虽然它仍然与您想要的输出略有不同,但我认为它有助于演示目的。
dt2 <- dt %>% mutate(vector = map_chr(type, toString))
dt2
# A tibble: 4 x 5
type typeof mode class vector
<list> <chr> <chr> <chr> <chr>
1 <chr [3]> character character character 1, 2, text
2 <int [2]> integer numeric integer 1, 2
3 <dbl [2]> double numeric numeric 1.5, 2.1
4 <lgl [2]> logical logical logical TRUE, FALSE