• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

R语言的对象以json的形式储存

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

参考

Getting started with JSON and jsonlite

0. 安装包

install.packages("rjson")

1. 向量

library(rjson)
vector = c(1:10)
vector
toJSON(vector)
> ### vector
> vector = c(1:10) # vector
> vector
 [1]  1  2  3  4  5  6  7  8  9 10
> vector_j = toJSON(vector)
> vector_j # json的形式
[1] "[1,2,3,4,5,6,7,8,9,10]"
> fromJSON(vector_j) # 转化为R
 [1]  1  2  3  4  5  6  7  8  9 10

2. 数据框

### data.frame
data("npk")
npk # R的数据框
npk_j = toJSON(npk)
npk_j # Json的数据框

fromJSON(npk_j) # 转化为R列表
as.data.frame(fromJSON(npk_j)) # 变为数据框
> ### data.frame
> data("npk")
> npk # R的数据框
   block N P K yield
1      1 0 1 1  49.5
2      1 1 1 0  62.8
3      1 0 0 0  46.8
4      1 1 0 1  57.0
5      2 1 0 0  59.8
6      2 1 1 1  58.5
7      2 0 0 1  55.5
8      2 0 1 0  56.0
9      3 0 1 0  62.8
10     3 1 1 1  55.8
11     3 1 0 0  69.5
12     3 0 0 1  55.0
13     4 1 0 0  62.0
14     4 1 1 1  48.8
15     4 0 0 1  45.5
16     4 0 1 0  44.2
17     5 1 1 0  52.0
18     5 0 0 0  51.5
19     5 1 0 1  49.8
20     5 0 1 1  48.8
21     6 1 0 1  57.2
22     6 1 1 0  59.0
23     6 0 1 1  53.2
24     6 0 0 0  56.0
> npk_j = toJSON(npk)
> npk_j # Json的数据框
[1] "{\"block\":[\"1\",\"1\",\"1\",\"1\",\"2\",\"2\",\"2\",\"2\",\"3\",\"3\",\"3\",\"3\",\"4\",\"4\",\"4\",\"4\",\"5\",\"5\",\"5\",\"5\",\"6\",\"6\",\"6\",\"6\"],\"N\":[\"0\",\"1\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"1\",\"1\",\"0\",\"1\",\"1\",\"0\",\"0\",\"1\",\"0\",\"1\",\"0\",\"1\",\"1\",\"0\",\"0\"],\"P\":[\"1\",\"1\",\"0\",\"0\",\"0\",\"1\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"1\",\"0\",\"1\",\"1\",\"0\",\"0\",\"1\",\"0\",\"1\",\"1\",\"0\"],\"K\":[\"1\",\"0\",\"0\",\"1\",\"0\",\"1\",\"1\",\"0\",\"0\",\"1\",\"0\",\"1\",\"0\",\"1\",\"1\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"1\",\"0\"],\"yield\":[49.5,62.8,46.8,57,59.8,58.5,55.5,56,62.8,55.8,69.5,55,62,48.8,45.5,44.2,52,51.5,49.8,48.8,57.2,59,53.2,56]}"
> 
> fromJSON(npk_j) # 转化为R列表
$`block`
 [1] "1" "1" "1" "1" "2" "2" "2" "2" "3" "3" "3" "3" "4" "4" "4" "4" "5"
[18] "5" "5" "5" "6" "6" "6" "6"

$N
 [1] "0" "1" "0" "1" "1" "1" "0" "0" "0" "1" "1" "0" "1" "1" "0" "0" "1"
[18] "0" "1" "0" "1" "1" "0" "0"

$P
 [1] "1" "1" "0" "0" "0" "1" "0" "1" "1" "1" "0" "0" "0" "1" "0" "1" "1"
[18] "0" "0" "1" "0" "1" "1" "0"

$K
 [1] "1" "0" "0" "1" "0" "1" "1" "0" "0" "1" "0" "1" "0" "1" "1" "0" "0"
[18] "0" "1" "1" "1" "0" "1" "0"

$yield
 [1] 49.5 62.8 46.8 57.0 59.8 58.5 55.5 56.0 62.8 55.8 69.5 55.0 62.0 48.8
[15] 45.5 44.2 52.0 51.5 49.8 48.8 57.2 59.0 53.2 56.0

> as.data.frame(fromJSON(npk_j)) # 变为数据框
   block N P K yield
1      1 0 1 1  49.5
2      1 1 1 0  62.8
3      1 0 0 0  46.8
4      1 1 0 1  57.0
5      2 1 0 0  59.8
6      2 1 1 1  58.5
7      2 0 0 1  55.5
8      2 0 1 0  56.0
9      3 0 1 0  62.8
10     3 1 1 1  55.8
11     3 1 0 0  69.5
12     3 0 0 1  55.0
13     4 1 0 0  62.0
14     4 1 1 1  48.8
15     4 0 0 1  45.5
16     4 0 1 0  44.2
17     5 1 1 0  52.0
18     5 0 0 0  51.5
19     5 1 0 1  49.8
20     5 0 1 1  48.8
21     6 1 0 1  57.2
22     6 1 1 0  59.0
23     6 0 1 1  53.2
24     6 0 0 0  56.0

3. 矩阵

### matrix
mat = matrix(1:9,3,3)
mat

mat_j = toJSON(mat,indent = "0")
mat_j
fromJSON(mat_j) 
matrix(fromJSON(mat_j),3,3)
> ### matrix
> mat = matrix(1:9,3,3)
> mat
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> 
> mat_j = toJSON(mat,indent = "0")
> mat_j
[1] "[1,2,3,4,5,6,7,8,9]"
> fromJSON(mat_j)
[1] 1 2 3 4 5 6 7 8 9
> matrix(fromJSON(mat_j),3,3)
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

4. 方差分析表

### 方差分析表
data(npk)
anova = summary(aov(yield~ N*P*K + block, data=npk))
anova

a_j = toJSON(anova)
a_j

as.data.frame(fromJSON(a_j))
> ### 方差分析表
> data(npk)
> anova = summary(aov(yield~ N*P*K + block, data=npk))
> anova
            Df Sum Sq Mean Sq F value  Pr(>F)   
N            1  189.3  189.28  12.259 0.00437 **
P            1    8.4    8.40   0.544 0.47490   
K            1   95.2   95.20   6.166 0.02880 * 
block        5  343.3   68.66   4.447 0.01594 * 
N:P          1   21.3   21.28   1.378 0.26317   
N:K          1   33.1   33.14   2.146 0.16865   
P:K          1    0.5    0.48   0.031 0.86275   
Residuals   12  185.3   15.44                   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> 
> a_j = toJSON(anova)
> a_j
[1] "[{\"Df\":[1,1,1,5,1,1,1,12],\"Sum Sq\":[189.281666666667,8.40166666666664,95.2016666666665,343.295,21.2816666666668,33.1350000000001,0.48166666666667,185.286666666667],\"Mean Sq\":[189.281666666667,8.40166666666664,95.2016666666665,68.659,21.2816666666668,33.1350000000001,0.48166666666667,15.4405555555556],\"F value\":[12.2587342136509,0.544129816860359,6.16568920231712,4.44666642679811,1.37829669341202,2.14597200733998,0.0311949051919551,\"NA\"],\"Pr(>F)\":[0.00437181182579937,0.474904092674435,0.0287950535002327,0.0159387902081939,0.263165282877167,0.168647878500492,0.862752085685407,\"NA\"]}]"
> 
> as.data.frame(fromJSON(a_j))
  Df      Sum.Sq     Mean.Sq F.value.12.2587342136509
1  1 189.2816667 189.2816667                 12.25873
2  1   8.4016667   8.4016667                 12.25873
3  1  95.2016667  95.2016667                 12.25873
4  5 343.2950000  68.6590000                 12.25873
5  1  21.2816667  21.2816667                 12.25873
6  1  33.1350000  33.1350000                 12.25873
7  1   0.4816667   0.4816667                 12.25873
8 12 185.2866667  15.4405556                 12.25873
  F.value.0.544129816860359 F.value.6.16568920231712
1                 0.5441298                 6.165689
2                 0.5441298                 6.165689
3                 0.5441298                 6.165689
4                 0.5441298                 6.165689
5                 0.5441298                 6.165689
6                 0.5441298                 6.165689
7                 0.5441298                 6.165689
8                 0.5441298                 6.165689
  F.value.4.44666642679811 F.value.1.37829669341202
1                 4.446666                 1.378297
2                 4.446666                 1.378297
3                 4.446666                 1.378297
4                 4.446666                 1.378297
5                 4.446666                 1.378297
6                 4.446666                 1.378297
7                 4.446666                 1.378297
8                 4.446666                 1.378297
  F.value.2.14597200733998 F.value.0.0311949051919551 F.value..NA.
1                 2.145972                 0.03119491           NA
2                 2.145972                 0.03119491           NA
3                 2.145972                 0.03119491           NA
4                 2.145972                 0.03119491           NA
5                 2.145972                 0.03119491           NA
6                 2.145972                 0.03119491           NA
7                 2.145972                 0.03119491           NA
8                 2.145972                 0.03119491           NA
  Pr..F..0.00437181182579937 Pr..F..0.474904092674435
1                0.004371812                0.4749041
2                0.004371812                0.4749041
3                0.004371812                0.4749041
4                0.004371812                0.4749041
5                0.004371812                0.4749041
6                0.004371812                0.4749041
7                0.004371812                0.4749041
8                0.004371812                0.4749041
  Pr..F..0.0287950535002327 Pr..F..0.0159387902081939
1                0.02879505                0.01593879
2                0.02879505                0.01593879
3                0.02879505                0.01593879
4                0.02879505                0.01593879
5                0.02879505                0.01593879
6                0.02879505                0.01593879
7                0.02879505                0.01593879
8                0.02879505                0.01593879
  Pr..F..0.263165282877167 Pr..F..0.168647878500492
1                0.2631653                0.1686479
2                0.2631653                0.1686479
3                0.2631653                0.1686479
4                0.2631653                0.1686479
5                0.2631653                0.1686479
6                0.2631653                0.1686479
7                0.2631653                0.1686479
8                0.2631653                0.1686479
  Pr..F..0.862752085685407 Pr..F...NA.
1                0.8627521          NA
2                0.8627521          NA
3                0.8627521          NA
4                0.8627521          NA
5                0.8627521          NA
6                0.8627521          NA
7                0.8627521          NA
8                0.8627521          NA

anova的Json格式, 转化为R格式时, 使用data.frame效果不太好.

如果有好的解决方案, 再更新!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
R语言实现LDA主题模型分析知乎话题发布时间:2022-07-18
下一篇:
R语言 列表发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap