假设您有一个不需要的列表列,您想将其转换为多个列。您没有提供可重现的数据,所以我将使用 iris 数据集创建这种情况。
使用数字列表列创建数据集
example_data <-
iris %>%
mutate(list_col = list(rnorm(7)))
此示例假设您在相关列中的数据实际上是一个列表。不需要的列表列称为list_col。所以示例数据如下所示:
Source: local data frame [150 x 6]
Groups: <by row>
# A tibble: 150 x 6
Sepal.Length Sepal.Width Petal.Length Petal.Width Species list_col
<dbl> <dbl> <dbl> <dbl> <fct> <list>
1 5.1 3.5 1.4 0.2 setosa <dbl [7]>
2 4.9 3 1.4 0.2 setosa <dbl [7]>
3 4.7 3.2 1.3 0.2 setosa <dbl [7]>
4 4.6 3.1 1.5 0.2 setosa <dbl [7]>
5 5 3.6 1.4 0.2 setosa <dbl [7]>
6 5.4 3.9 1.7 0.4 setosa <dbl [7]>
7 4.6 3.4 1.4 0.3 setosa <dbl [7]>
8 5 3.4 1.5 0.2 setosa <dbl [7]>
9 4.4 2.9 1.4 0.2 setosa <dbl [7]>
10 4.9 3.1 1.5 0.1 setosa <dbl [7]>
# ... with 140 more rows
创建列名
接下来,告诉 R 你要添加的新列名。
my_new_column_names <- c("TA0", "TA1", "TA2", "TA3", "TA4", "TA5", "TA6")
请注意我添加到iris 以创建example_data 的随机列表的长度等于我们假设的新列(名称)的数量。此外,数据帧每一行的列表长度是相同的。
隔离列表数据(即列表列)并操作到新的数据帧中
new_df <- example_data %>%
pull(list_col) %>%
map(. , ~{setNames(.x, my_new_column_names)}) %>%
map(., ~{.x %>% t %>% as.tibble}) %>%
bind_rows()
这里,pull() 只是隔离了列表列,然后第一个 map() 将每个列表元素的名称设置为 my_new_column_names 中指定的名称。第二个map() 将列表转换为数据帧。对于每个列表(即原始example_data 的每一行),我们不想要具有 7 个值和命名行的单列数据帧,我们想要具有 7 列的单行数据帧,因此我们需要 t 函数转置每个列表。
最后的 bind_rows() 只是将 1×7 数据帧列表转换为单个多行数据帧。
(这一步可能会有所改进,但这应该可以。)
将新数据添加回原始数据
最后一步是将现在正确格式化的数据添加回原始数据,并删除现在多余的列表列。
corrected_data <- example_data %>%
bind_cols(new_df) %>%
select(-list_col)
corrected_data 现在应该是这样的
Source: local data frame [150 x 12]
Groups: <by row>
# A tibble: 150 x 12
Sepal.Length Sepal.Width Petal.Length Petal.Width Species TA0 TA1
<dbl> <dbl> <dbl> <dbl> <fct> <dbl> <dbl>
1 5.1 3.5 1.4 0.2 setosa -0.170 0.225
2 4.9 3 1.4 0.2 setosa 1.32 -2.00
3 4.7 3.2 1.3 0.2 setosa -0.517 1.60
4 4.6 3.1 1.5 0.2 setosa 0.0867 -0.473
5 5 3.6 1.4 0.2 setosa 0.138 0.213
6 5.4 3.9 1.7 0.4 setosa -0.213 0.892
7 4.6 3.4 1.4 0.3 setosa -0.804 -1.34
8 5 3.4 1.5 0.2 setosa 0.754 -2.57
9 4.4 2.9 1.4 0.2 setosa -0.138 1.11
10 4.9 3.1 1.5 0.1 setosa -0.814 1.24
# ... with 140 more rows, and 5 more variables: TA2 <dbl>, TA3 <dbl>,
# TA4 <dbl>, TA5 <dbl>, TA6 <dbl>
...我认为这是你想要的。
如果你想把它全部粉碎成一串管道函数,它看起来像
example_data %>%
pull(list_col) %>%
map(. , ~{setNames(.x, my_new_column_names)}) %>%
map(., ~{.x %>% t %>% as.tibble}) %>%
bind_rows() %>%
bind_cols(example_data, .) %>%
select(-list_col)