R-JSON 파일
JSON 파일은 데이터를 사람이 읽을 수있는 형식의 텍스트로 저장합니다. Json은 JavaScript Object Notation을 나타냅니다. R은 rjson 패키지를 사용하여 JSON 파일을 읽을 수 있습니다.
rjson 패키지 설치
R 콘솔에서 다음 명령을 실행하여 rjson 패키지를 설치할 수 있습니다.
install.packages("rjson")입력 데이터
아래 데이터를 메모장과 같은 텍스트 편집기에 복사하여 JSON 파일을 만듭니다. 파일을.json 확장자 및 파일 유형 선택 all files(*.*).
{ 
   "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 파일 읽기
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"JSON을 데이터 프레임으로 변환
위에서 추출한 데이터를 R 데이터 프레임으로 변환하여 추가 분석을 위해 as.data.frame() 함수.
# 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