在R中将Dataframe转换为List of List(Python等效于字典)

s8vozzvw  于 2023-01-15  发布在  Python
关注(0)|答案(2)|浏览(146)

我有一个 Dataframe :

location <- c("a", "b", "c", "d", "e", "e")
type <- c("city", "city", "town", "town", "village", "village")
code <- c("123", "112", "83749", "83465", "38484757", "3838891")
country <- c("zz", "zz", "zz", "zz", "zz", "zz")
df <- data.frame(location, type, code, country)

我想按位置分组并转换为字典类似下面的东西:

{location:[[type], [code], [country]]}

我知道使用python应该很简单,但是我不知道如何使用R来实现。我已经尝试了下面的unclass,但是仍然没有得到我所期望的:

unclass(by(df, df$location, function(x) {
  tmp <- x$code
  setNames(tmp, x$location[1])
  tmp
})) -> location_mapping

预期输出:

{
'a':[['city'],['123'],['zz']],
'b':[['city'],['112'],['zz']],
'c':[['town'],['83749'],['zz']],
'd':[['town'],['83465'],['zz']],
'e':[['village'],['38484757','3838891'],['zz']]
}
khbbv19g

khbbv19g1#

---已编辑

从你更新的问题来看,下面的代码可能就是你想要的,R不像python那样使用花括号,但是为了给更多的函数提供信息,下面的代码可以满足你的需要:

library(dplyr)

location <- c("a", "b", "c", "d", "e", "e")
type <- c("city", "city", "town", "town", "village", "village")
code <- c("123", "112", "83749", "83465", "38484757", "3838891")
country <- c("zz", "zz", "zz", "zz", "zz", "zz")
df <- data.frame(location, type, code, country)

df %>% 
  dplyr::group_by(location) %>% 
  summarise(code=list(code), across()) %>% # makes list of multiple `code` entries / `across()` keeps cols
  filter(!duplicated(location)) %>% # filtering duplicate locations
  .[,c(1,3,2,4] # arranging cols

# A tibble: 5 × 4
# Groups:   location [5]
  location type    code      country
  <chr>    <chr>   <list>    <chr>  
1 a        city    <chr [1]> zz     
2 b        city    <chr [1]> zz     
3 c        town    <chr [1]> zz     
4 d        town    <chr [1]> zz     
5 e        village <chr [2]> zz
yacmzcpb

yacmzcpb2#

您可以跨多个列汇总locationunique()的每组。

library(dplyr)

dict <- df %>% 
  group_by(country, type, location) %>% 
  summarise(code = list(code), .groups = "drop")

dict
# # A tibble: 5 × 4
#   country type    location code     
#   <chr>   <chr>   <chr>    <list>
# 1 zz      city    a        <chr [1]>
# 2 zz      city    b        <chr [1]>
# 3 zz      town    c        <chr [1]>
# 4 zz      town    d        <chr [1]>
# 5 zz      village e        <chr [2]>

在将其转换为JSON之后,可以得到预期的结构。

split(select(dict, -location), dict$location) %>%
  jsonlite::toJSON(dataframe = "values", pretty = TRUE, auto_unbox = TRUE)

# {
#   "a": [["zz", "city", "123"]],
#   "b": [["zz", "city", "112"]],
#   "c": [["zz", "town", "83749"]],
#   "d": [["zz", "town", "83465"]],
#   "e": [["zz", "village", ["38484757", "3838891"]]]
# }

相关问题