R -键值对列表的两个数据框列

chy5wohz  于 2024-01-03  发布在  其他
关注(0)|答案(2)|浏览(107)

假设我有一个 Dataframe

DF1 <- data.frame("a" = c("a", "b", "c"), "b" = 1:3)

字符串
最简单的方法是什么把它变成一个列表?

DF2 <- list("a" = 1, "b" = 2, "c" = 3)


这一定很简单,但我找不到答案。

7cwmlq89

7cwmlq891#

您可以使用setNamesas.list

DF2 <- setNames(as.list(DF1$b), DF1$a)

字符串

b09cbbtk

b09cbbtk2#

2023年的整洁方法是使用deframe:

tibble(
  key = c("key1", "key2", "key3"),
  value = c("val1", "val2", "val3")
) %>%
deframe()

#   key1   key2   key3 
# "val1" "val2" "val3" 

tibble(
  key = c("key1", "key2", "key3"),
  value = c("val1", "val2", "val3")
) %>%
deframe() %>%
as.list()

# $key1
# [1] "val1"
#
# $key2
# [1] "val2"
#
# $key3
# [1] "val3"

字符串

相关问题