假设我有一个 Dataframe
DF1 <- data.frame("a" = c("a", "b", "c"), "b" = 1:3)
字符串最简单的方法是什么把它变成一个列表?
DF2 <- list("a" = 1, "b" = 2, "c" = 3)
型这一定很简单,但我找不到答案。
7cwmlq891#
您可以使用setNames和as.list
setNames
as.list
DF2 <- setNames(as.list(DF1$b), DF1$a)
字符串
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"
2条答案
按热度按时间7cwmlq891#
您可以使用
setNames
和as.list
字符串
b09cbbtk2#
2023年的整洁方法是使用deframe:
字符串