gpt4 book ai didi

r - 如何展平从 jsonlite 返回的嵌套数据帧

转载 作者:行者123 更新时间:2023-12-01 12:39:20 24 4
gpt4 key购买 nike

我正在用 jsonlite 加载这个 JSON 数据

<snip>  
"rawData": {
"fortune": {},
"plaintext": {},
"db": {},
"update": {
"duda": [
{
"latencyAvg": "201.40us",
"latencyMax": "727.00us",
"latencyStdev": "54.85us",
"totalRequests": 561810,
"startTime": 1413890149,
"endTime": 1413890164
}
]
},
"json": {
"duda": [
{
"latencyAvg": "201.40us",
"latencyMax": "727.00us",
"latencyStdev": "54.85us",
"totalRequests": 561810,
"startTime": 1413890149,
"endTime": 1413890164
}
]
},
"query": {}
}

这导致具有嵌套数据框的结构
data <- structure(list(fortune = structure(list(), .Names = character(0)), 
plaintext = structure(list(), .Names = character(0)), db = structure(list(), .Names = character(0)),
update = structure(list(duda = structure(list(latencyAvg = "201.40us",
latencyMax = "727.00us", latencyStdev = "54.85us", totalRequests = 561810L,
startTime = 1413890149L, endTime = 1413890164L), .Names = c("latencyAvg",
"latencyMax", "latencyStdev", "totalRequests", "startTime",
"endTime"), class = "data.frame", row.names = 1L)), .Names = "duda"),
json = structure(list(duda = structure(list(latencyAvg = "201.40us",
latencyMax = "727.00us", latencyStdev = "54.85us", totalRequests = 561810L,
startTime = 1413890149L, endTime = 1413890164L), .Names = c("latencyAvg",
"latencyMax", "latencyStdev", "totalRequests", "startTime",
"endTime"), class = "data.frame", row.names = 1L)), .Names = "duda"),
query = structure(list(), .Names = character(0))), .Names = c("fortune",
"plaintext", "db", "update", "json", "query"))

我想创建一个如下所示的 data.frame:
Type   | Name | latencyAvg | latencyMax | latencyStdev | totalRequests | startTime | endTime
json | duda | 201.40us | <etc..>
update | duda | 201.40us | <etc..>

通过展平嵌套的数据框。我正在弄清楚如何通过删除我想要的项目并使用 rbind/cbind 将它们移动到新的数据框中来手动执行此操作,但是有没有一种简单的方法来执行这种类型的递归展平?

最佳答案

假设该对象被命名为“数据”的不幸名称:

newdat <- rbind.data.frame( Type= c(rep("json",   nrow(data$json$duda)), 
rep("update", nrow(data$update$duda)) ),
rbind( data$json$duda, data$update$duda) )

关于r - 如何展平从 jsonlite 返回的嵌套数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26498185/

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