[英]How to convert a list column to a vector in a tibble in R
I am trying to collapse the Weeks column in the tibble (called data) below so that the result is a tibble with two columns: 1 for weeks and another for associated values. 我正在尝试折叠下面的小标题(称为数据)中的Weeks列,以便结果是带有两列的小标题:1表示星期,另一栏表示关联的值。 Since the weeks column is a list, I am not sure how to do it.
由于周列是一个列表,因此我不确定该如何做。 Could you please help?
能否请你帮忙?
> head(data)
# A tibble: 6 x 1
``$Weeks $Value
<list> <dbl>
1 <date [9]> 30
2 <date [3]> 20
3 <date [3]> 15
4 <date [5]> 10
5 <date [2]> 9
6 <date [9]> 5
desired result would look like: 所需的结果如下所示:
$Weeks $Value
<Date> <dbl>
1 "2019-01-01 30
2 "2019-01-08 30
3 "2019-01-15 30
etc..
Current Structure: 当前结构:
> str(data)
List of 1
$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 7 obs. of 2 variables:
..$ Weeks:List of 7
.. ..$ : Date[1:9], format: "2018-11-11" "2018-11-18" "2018-11-25" "2018-12-02" ...
.. ..$ : Date[1:3], format: "2018-12-02" "2018-12-09" "2018-12-16"
.. ..$ : Date[1:3], format: "2018-12-23" "2018-12-30" "2019-01-06"
.. ..$ : Date[1:5], format: "2018-11-04" "2018-11-11" "2018-11-18" "2018-11-25" ...
.. ..$ : Date[1:2], format: "2018-11-25" "2018-12-02"
.. ..$ : Date[1:9], format: "2018-11-18" "2018-11-25" "2018-12-02" "2018-12-09" ...
.. ..$ : Date[1:14], format: "2018-09-30" "2018-10-07" "2018-10-14" "2018-10-21" ...
..$ Value: num [1:7] 30 20 15 10 9 5 6
dput(data) dput(数据)
> dput(data)
list(structure(list(Weeks = list(structure(c(17846, 17853, 17860,
17867, 17874, 17881, 17888, 17895, 17902), class = "Date"), structure(c(17867,
17874, 17881), class = "Date"), structure(c(17888, 17895, 17902
), class = "Date"), structure(c(17839, 17846, 17853, 17860, 17867
), class = "Date"), structure(c(17860, 17867), class = "Date"),
structure(c(17853, 17860, 17867, 17874, 17881, 17888, 17895,
17902, 17909), class = "Date"), structure(c(17804, 17811,
17818, 17825, 17832, 17839, 17846, 17853, 17860, 17867, 17874,
17881, 17888, 17895), class = "Date")), Value = c(30, 20,
15, 10, 9, 5, 6)), row.names = c(NA, -7L), class = c("tbl_df",
"tbl", "data.frame")))
Edited Answer 编辑答案
Ok, so your original data is stored as a list, which doesn't allow the unnest()
function to behave as expected. 好的,因此您的原始数据存储为列表,这不允许
unnest()
函数按预期运行。 We'll need to extract the element first. 我们需要先提取元素。 Then, because your list-column is itself a list of lists, we'll have to use
map
to extract out what we want. 然后,由于您的list-column本身就是列表的列表,因此我们必须使用
map
提取所需的内容。 The solution below solves this problem, and gives you what you want. 下面的解决方案可以解决此问题,并为您提供所需的内容。
> data[[1]] %>%
mutate(Weeks = map(Weeks, ~ tibble(Weeks =.x))) %>%
unnest()
Output: 输出:
# A tibble: 45 x 2
Value Weeks
<dbl> <date>
1 30 2018-11-11
2 30 2018-11-18
3 30 2018-11-25
4 30 2018-12-02
5 30 2018-12-09
6 30 2018-12-16
7 30 2018-12-23
8 30 2018-12-30
9 30 2019-01-06
10 20 2018-12-02
Original Answer: 原始答案:
As akrun said in the comments, you can just do unnest(data, Weeks)
. 正如akrun在评论中所说,您可以只进行
unnest(data, Weeks)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.