【发布时间】:2020-11-13 09:55:04
【问题描述】:
我有一个列表,我需要每个单独的元素作为一个字符。
> str(df_data2)
'data.frame': 2 obs. of 4 variables:
$ retweet_count: int 21 31
$ quote_count : int NA NA
$ reply_count : int NA NA
$ hashtags :List of 2
..$ : chr "BigData" "DataScience"
..$ : chr "python" "neuralnetworks"
该列表的行数与数据集相同,但是,我希望每个组件都有字符串值。
> str(df_data2)
'data.frame': 2 obs. of 4 variables:
$ retweet_count: int 21 31
$ quote_count : int NA NA
$ reply_count : int NA NA
$ hashtags : chr "BigData DataScience" "python neuralnetworks"
我该怎么做?
【问题讨论】:
-
请使用
dput()发布您的示例数据。 -
@MartinGal 使用
unnest将为列表的每个元素创建一个新行。 -
@IanCampbell 自己捂脸。好点子!
-
@MartinGal 我觉得应该有一个
tidyr::hoist选项,但我想不通。 -
@IanCampbell 我也不是。至少不是一行。我最好的方法是
unnest_wider与unite连接。但是你已经给出了一个不错的解决方案。
标签: r list type-conversion character