问题
I am loading this JSON data with jsonlite
<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": {}
}
Which results in a structure with nested data frames
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"))
I'd like to create a single data.frame that looks like this:
Type | Name | latencyAvg | latencyMax | latencyStdev | totalRequests | startTime | endTime
json | duda | 201.40us | <etc..>
update | duda | 201.40us | <etc..>
By flatting the nested data frames. I'm figuring out how to do this manually by removing the items I want and using rbind/cbind to move them into a new data frame, but is there a simple way to do this type of recursive flattening?
回答1:
Assuming that object is named the unfortunate name of "data":
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) )
回答2:
Another oneliner
do.call(rbind, lapply(data[c('json', 'update')], '[[', 'duda'))
回答3:
use flatten
function. it gets a data frame and returns a flat data frame.
if you need to exclude some columns from source data frame then use df[[-i]]
to exclude column i
.
来源:https://stackoverflow.com/questions/26498185/how-to-flatten-nested-data-frames-returned-from-jsonlite