如何在R中反规范化嵌套列表? [英] How to denormalize nested list in R?
问题描述
我想找到一种清晰易读的方式将类似src
的结构(从JSON解析,这就是为什么有大量嵌套列表的原因)转换成类似dst
的结构的方式.
I'd like to find a clean and readable way to convert a structure like src
(parsed from JSON, that's why there are a ton of nested lists) to one like dst
.
src <- list(
sessions=list(
list(
statistics=list(
list(
list(
round=0,
diff=3,
saldo=3
),
list(
round=1,
diff=-1,
saldo=2
),
list(
round=2,
diff=-1,
saldo=1
)
),
list(
list(
round=0,
diff=-1,
saldo=-1
)
)
),
sessionProp="sv1"
),
list(
statistics=list(
list(
list(
round=0,
diff=4,
saldo=4
)
),
list(
list(
round=0,
diff=2,
saldo=2
)
)
),
sessionProp="sv2"
)
),
packageProps=list(
rules=list(
list(
name="Ruleset 1",
ruleProp="rv1"
),
list(
name="Ruleset 2",
ruleProp="rv2"
)
),
packageProp="package prop value"
)
)
dst <- data.frame(
round=c(0,1,2,0,0,0),
diff=c(3,-1,-1,-1,4,2),
saldo=c(3,2,1,-1,4,2),
sessionProp=c("sv1","sv1","sv1","sv1","sv2","sv2"),
ruleProp=c("rv1","rv1","rv1","rv2","rv1","rv2")
)
两个会话在statistics
列表中都有两个元素.这些元素对应于rules
列表packageProps
中的两个元素.这是与几乎直接非正规化的唯一区别.
Both sessions have two elements in statistics
list. Those correspond to two elements in rules
list packageProps
. That's the only difference from pretty much direct denormalization.
推荐答案
在更大的列表上试一试(我假设有更多或更大的版本)
Give this a shot on the larger list (I'm assuming there are either more or larger versions of this):
do.call(rbind.data.frame, lapply(1:length(src$sessions), function(i) {
dat <- do.call(rbind.data.frame,
lapply(unlist(src$sessions[[i]]$statistics, recursive=FALSE),
rbind.data.frame))
dat$sessionProp <- src$sessions[[i]]$sessionProp
dat$ruleProp <- src$packageProps$rules[[i]]$ruleProp
dat
}))
## round diff saldo sessionProp ruleProp
## 2 0 3 3 sv1 rv1
## 21 1 -1 2 sv1 rv1
## 22 2 -1 1 sv1 rv1
## 23 0 -1 -1 sv1 rv1
## 24 0 4 4 sv2 rv2
## 211 0 2 2 sv2 rv2
如果需要,可以删除行名称.如果您使用dplyr
的bind_rows
与do.call(rbind…
,则只会节省一些输入时间,而且还会自动对行名进行定位.不过,我希望其他人可以找到更好的解决方案.
You can nuke the row names if desired. If you use dplyr
's bind_rows
vs do.call(rbind…
it only saves a teensy bit of typing, but will also auto-nuke the row names. I'm hoping others can find an even more optimal solution, though.
这篇关于如何在R中反规范化嵌套列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!