簡體   English   中英

JSON鍵,使用R的值格式/選擇列作為鍵,另一列作為值

[英]JSON key, value formatting with R /choose a column as key and another one as value

我有一個數據框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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM