gpt4 book ai didi

r - 将具有混合内容的命名列表转换为数据框

转载 作者:行者123 更新时间:2023-12-04 03:41:57 26 4
gpt4 key购买 nike

有没有更好,更好的方法将具有混合内容的命名列表转换为数据框?

工作示例:

my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)

my_df <- data.frame(
"key" = names(my_list),
stringsAsFactors = F
)

my_df[["value"]] <- unname(my_list)

是否可以一步完成此转换?

最佳答案

我们可以使用stack中的base R

stack(my_list)

根据 ?stack

The stack function is used to transform data available as separate columns in a data frame or list into a single column that can be used in an analysis of variance model or other linear model. The unstack function reverses this operation.



或使用 enframe
library(tidyverse)
enframe(my_list) %>% # creates the 'value' as a `list` column
mutate(value = map(value, as.character)) %>% # change to single type
unnest

关于r - 将具有混合内容的命名列表转换为数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53230519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com