gpt4 book ai didi

r - JSON 键,使用 R 进行值格式化/选择一列作为键,另一列作为值

转载 作者:行者123 更新时间:2023-12-03 19:35:26 24 4
gpt4 key购买 nike

我有一个数据框 gscSocial ,我正在尝试将其转换为 JSON:

  Date       totalReach
2018-01-01 1069
2018-01-02 3075
2018-01-03 2674
2018-01-04 876

这就是我正在做的:
   gscSocialTotalReach   <- gscSocial %>%
as.tibble()%>%
gather(key=Date, value=totalReach) %>%
jsonlite::toJSON(auto_unbox = TRUE)

结果是这样的:
[{"Date":"2018-01-01","totalReach":1069},{"Date":"2018-01-02","totalReach":3075},{"Date":"2018-01-03","totalReach":2674},{"Date":"2018-01-04","totalReach":876}]

但我想要这个:
[{"2018-01-01":1069,"2018-01-02":3075,"2018-01-03":2674,"2018-01-04":876}]

我怎样才能做到这一点?谢谢

最佳答案

根据@thelatemail 的建议,代码是:

toJSON(setNames(as.list(gscSocial$totalReach), gscSocial$Date),  auto_unbox = TRUE)

关于r - JSON 键,使用 R 进行值格式化/选择一列作为键,另一列作为值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49847888/

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