JSON文件以人类可读格式将数据存储为文本。 Json代表JavaScript Object Notation。 R可以使用rjson包读取JSON文件。
10多年的雁塔网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销的优势是能够根据用户设备显示端的尺寸不同,自动调整雁塔建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“雁塔网站设计”,“雁塔网站推广”以来,每个客户项目都认真落实执行。
在r语言控制台中,您可以发出以下命令来安装rjson包。
install.packages("rjson")
通过将以下数据复制到文本编辑器(如记事本)中来创建JSON文件。 使用.json扩展名保存文件,并将文件类型选择为所有文件(*.*)。
{ "ID":["1","2","3","4","5","6","7","8" ], "Name":["Rick","Dan","Michelle","Ryan","Gary","Nina","Simon","Guru" ], "Salary":["623.3","515.2","611","729","843.25","578","632.8","722.5" ], "StartDate":[ "1/1/2012","9/23/2013","11/15/2014","5/11/2014","3/27/2015","5/21/2013", "7/30/2013","6/17/2014"], "Dept":[ "IT","Operations","IT","HR","Finance","IT","Operations","Finance"] }
JSON文件由R使用来自JSON()的函数读取。 它作为列表存储在R中。
# Load the package required to read JSON files. library("rjson") # Give the input file name to the function. result <- fromJSON(file = "input.json") # Print the result. print(result)
当我们执行上面的代码,它产生以下结果 -
$ID [1] "1" "2" "3" "4" "5" "6" "7" "8" $Name [1] "Rick" "Dan" "Michelle" "Ryan" "Gary" "Nina" "Simon" "Guru" $Salary [1] "623.3" "515.2" "611" "729" "843.25" "578" "632.8" "722.5" $StartDate [1] "1/1/2012" "9/23/2013" "11/15/2014" "5/11/2014" "3/27/2015" "5/21/2013" "7/30/2013" "6/17/2014" $Dept [1] "IT" "Operations" "IT" "HR" "Finance" "IT" "Operations" "Finance"
我们可以使用as.data.frame()函数将上面提取的数据转换为R语言数据帧以进行进一步分析。
# Load the package required to read JSON files. library("rjson") # Give the input file name to the function. result <- fromJSON(file = "input.json") # Convert JSON file to a data frame. json_data_frame <- as.data.frame(result) print(json_data_frame)
当我们执行上面的代码,它产生以下结果 -
id, name, salary, start_date, dept 1 1 Rick 623.30 2012-01-01 IT 2 2 Dan 515.20 2013-09-23 Operations 3 3 Michelle 611.00 2014-11-15 IT 4 4 Ryan 729.00 2014-05-11 HR 5 NA Gary 843.25 2015-03-27 Finance 6 6 Nina 578.00 2013-05-21 IT 7 7 Simon 632.80 2013-07-30 Operations 8 8 Guru 722.50 2014-06-17 Finance
名称栏目:创新互联R语言教程:R语言JSON文件
文章路径:http://www.mswzjz.cn/qtweb/news9/294059.html
攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能