R 如何在TIBLE中显示列表的元素?
我有以下几点:R 如何在TIBLE中显示列表的元素?,r,purrr,tibble,R,Purrr,Tibble,我有以下几点: 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
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
甚至不确定
是什么…首先,如果您使用的是purr
软件包,则在创建示例数据框时可能不需要取消列表。我们可以使用map\u 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))
没有比在类型中存储它更好的方法了。将其存储为代码的字符向量是个坏主意。要查看它,只需提取它,例如,df$type
,或者使用tidyr::unnest
来扩展它(在这种情况下,这是一个坏主意,因为类型不一致)。@alistaire我只是试图打印一个表格,以了解这三个函数之间的差异
# 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>
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))
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