将一列列表取消嵌套到 tidyr 中的多列 [英] Unnest one column list to many columns in tidyr

查看:27
本文介绍了将一列列表取消嵌套到 tidyr 中的多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有这样一个整洁的数据框:

df <- tibble(id=1:2,ctn=list(list(a="x",b=1),列表(a="y",b=2)))# 小块:2 x 2身份证号<int><列表>1 1 <列表[2]>2 2 <列表[2]>

如何将 ctn 列取消嵌套到右侧,以便数据框如下所示:

# tibble: 2 x 3身份证号<int><chr><dbl>1 1 × 12 2 年 2

解决方案

With dplyrpurrr

df %>%变异(ctn = map(ctn,as_tibble))%>%取消嵌套()

<块引用>

# tibble: 2 x 3身份证号<int><chr><dbl>1 1 × 12 2 年 2

For example, I have a tidy data frame like this:

df <- tibble(id=1:2,
         ctn=list(list(a="x",b=1),
                  list(a="y",b=2)))
# A tibble: 2 x 2
     id        ctn
  <int>     <list>
1     1 <list [2]>
2     2 <list [2]>

How could I unnest ctn column to the right so that the data frame will be like this:

# A tibble: 2 x 3
     id     a     b
  <int> <chr> <dbl>
1     1     x     1
2     2     y     2

解决方案

With dplyr and purrr

df %>% 
  mutate(ctn = map(ctn, as_tibble)) %>%
  unnest()

# A tibble: 2 x 3
     id     a     b
  <int> <chr> <dbl>
1     1     x     1
2     2     y     2

这篇关于将一列列表取消嵌套到 tidyr 中的多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆