数据框中标有ID列的列表列 [英] Flatten list column in data frame with ID column

查看:117
本文介绍了数据框中标有ID列的列表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框包含具有选择多个问题类型的调查的输出。一些单元格有多个值。

My data frame contains the output of a survey with a select multiple question type. Some cells have multiple values.

df <- data.frame(a=1:3,b=I(list(1,1:2,1:3)))
df
  a       b
1 1       1
2 2    1, 2
3 3 1, 2, 3

我想平整列表以获取以下输出:

I would like to flatten out the list to obtain the following output:

df
  a       b
1 1       1
2 2       1
3 2       2
4 3       1
5 3       2
6 3       3

应该很简单,但不知何故我不能找到搜索词。谢谢。

should be easy but somehow I can't find the search terms. thanks.

推荐答案

您可以使用tidyr中的 unnest

You can just use unnest from "tidyr":

library(tidyr)
unnest(df, b)
#   a b
# 1 1 1
# 2 2 1
# 3 2 2
# 4 3 1
# 5 3 2
# 6 3 3

这篇关于数据框中标有ID列的列表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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