[英]Creating a json string from a loadings object in R
After performing a factor analysis the loadings object looks like this: 执行因子分析后,加载对象看起来像这样:
Loadings:
Factor1 Factor2
IV1 0.844 -0.512
IV2 0.997
IV3 -0.235
IV4 -0.144
IV5 0.997
Factor1 Factor2
SS loadings 1.719 1.333
Proportion Var 0.344 0.267
Cumulative Var 0.344 0.610
I can target the factors themselves using print(fit$loadings[,1:2])
to get the following. 我可以使用
print(fit$loadings[,1:2])
来定位因子本身以获得以下内容。
Factor1 Factor2
IV1 0.84352949 -0.512090197
IV2 0.01805673 0.997351400
IV3 0.05877499 -0.234710743
IV4 0.09088599 -0.144251843
IV5 0.99746785 0.008877643
I would like to create a json string that would look something like the following. 我想创建一个类似于以下内容的json字符串。
"loadings": {
"Factor1": {
"IV1": 0.84352949, "IV2":0.01805673, "IV3":0.05877499, "IV4": 0.09088599, "IV5": 0.99746785
},
"Factor2": {
"IV1": -0.512090197, "IV2": 0.997351400, "IV3": -0.234710743, "IV4": -0.144251843, "IV5": 0.008877643
}
}
I have tried accessing the individual properties using unclass(), hoping that I could then loop through and put them into a string,have not had any luck ( using loads <- loadings(fit)
and <- names(unclass(loads))
names shows up as "null") 我尝试使用unclass()访问单个属性,希望然后可以遍历并将它们放入字符串,没有任何运气(使用
loads <- loadings(fit)
<- names(unclass(loads))
loads <- loadings(fit)
和<- names(unclass(loads))
名称显示为“null”)
Just seconding @GSee's comment (+1) and @dickoa's answer (+1) with a closer example: 只需附上一个更接近的示例,即可附上@GSee的评论(+1)和@dickoa的答案(+1):
Creating some demo data for reproducible example (you should also provide one in all your Qs): 为可重现的示例创建一些演示数据(您还应该在所有Q中提供一个):
> fit <- princomp(~ ., data = USArrests, scale = FALSE)
Load RJSONIO
/ rjson
packages: 加载
RJSONIO
/ rjson
包:
> library(RJSONIO)
Transform your data to fit your needs: 转换数据以满足您的需求:
> res <- list(loadings = apply(fit$loadings, 2, list))
Return JSON: 返回JSON:
> cat(toJSON(res)) { "loadings": { "Comp.1": [ { "Murder": -0.041704, "Assault": -0.99522, "UrbanPop": -0.046336, "Rape": -0.075156 } ], "Comp.2": [ { "Murder": 0.044822, "Assault": 0.05876, "UrbanPop": -0.97686, "Rape": -0.20072 } ], "Comp.3": [ { "Murder": 0.079891, "Assault": -0.06757, "UrbanPop": -0.20055, "Rape": 0.97408 } ], "Comp.4": [ { "Murder": 0.99492, "Assault": -0.038938, "UrbanPop": 0.058169, "Rape": -0.072325 } ] } }>
You can do something along these lines 你可以沿着这些方向做点什么
require(RJSONIO) ## or require(rjson)
pca <- prcomp(~ ., data = USArrests, scale = FALSE)
export <- list(loadings = split(pca$rotation, rownames(pca$rotation)))
cat(toJSON(export))
## {
## "loadings": {
## "Assault": [ 0.99522, -0.05876, -0.06757, 0.038938 ],
## "Murder": [ 0.041704, -0.044822, 0.079891, -0.99492 ],
## "Rape": [ 0.075156, 0.20072, 0.97408, 0.072325 ],
## "UrbanPop": [ 0.046336, 0.97686, -0.20055, -0.058169 ]
## }
## }
If you want to export it : 如果要导出它:
cat(toJSON(export), file = "loadings.json")
If it doesn't really suit your need, just modify the data structure ( export
object) to the output you want. 如果它不能满足您的需要,只需将数据结构(
export
对象)修改为您想要的输出即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.