JSON键,使用R的值格式/选择列作为键,另一列作为值

时间:2018-04-15 23:46:57

标签: r json

我有一个数据框 gscSocialTotalReach <- gscSocial %>% as.tibble()%>% gather(key=Date, value=totalReach) %>% jsonlite::toJSON(auto_unbox = TRUE) ,我试图将其转换为JSON:

[{"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}]

结果如下:

{{1}}

但我想这样:

{{1}}

我该怎么做?感谢

1 个答案:

答案 0 :(得分:1)

根据@thelatemail建议代码是:

toJSON(setNames(as.list(gscSocial$totalReach), gscSocial$Date),  auto_unbox = TRUE)
相关问题