R语言开发之JSON文件处理操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/luyaran/article/details/82767696

JSON文件以可读取的格式将数据存储为文本,它是一种JavaScript对象表示法,在R中可以使用rjson包读取JSON文件。

我们通过将以下数据复制到文本编辑器(如记事本)中创建一个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"]
}

在R中使用JSON()函数读取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)

我们可以将上述提取的数据通过使用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)

好啦,本次记录就到这里了。

如果感觉不错的话,请多多点赞支持哦。。。

猜你喜欢

转载自blog.csdn.net/luyaran/article/details/82767696