반응형

/*******************************************************************************************************************
-- Title : [MSR] ScaleR - 파일 읽기/쓰기(CSV, XDF, TXT, XLS)
-- Reference : microsoft.com
-- Key word : microsoft r revoscale r scale r revoscaler scaler getwd setwd rxgetoption file.path rximport
                  rxxdfdata data.frame dataframe 데이터프레임 데이터 프레임 csv xdf txt xls xlsx file
*******************************************************************************************************************/

-- Microsoft R

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# ***********************************************
# -- 경로 및 파일명 처리
# ***********************************************
 
# ------------------------------
# -- R 이용
# ------------------------------
 
# -- 현재 디폴트 경로
getwd()
 
# -- 디폴트 경로 수정
#    Visual Studio > Rtools > Work Directory > Select Work ...와 동일
setwd("C:\\RProject\\MRS\\Data")
getwd()
 
defaultwd = getwd()
defaultwd
 
# ------------------------------
# -- RevoScaleR 이용 
# ------------------------------
 
# -- 샘플 패키지 경로 지정
#    Get the location of the sample data sets for RevoScaleR
sampleDataDir <- rxGetOption("sampleDataDir")
sampleDataDir                                                   # 경로 확인
 
# -- FullPath + Filename
inputFile <- file.path(defaultwd, "AirlineDemoSmall.csv")
inputFile                                                       # 경로 + 파일명
 
inputFile2 = file.path("c:\\""AirlineDemoSmall.csv")
inputFile2 
 
 
# ***********************************************
# -- CSV 읽기/쓰기
# ***********************************************
 
# ------------------------------
# -- R 이용한 CSV 읽기/쓰기
# ------------------------------
 
# http://dbrang.tistory.com/1036
 
# ------------------------------
# -- CSV 읽기(rxImport)
# ------------------------------
 
# -- 디폴트 경로 수정
#    Visual Studio > Rtools > Work Directory > Select Work ...와 동일
setwd("C:\\RProject\\MRS\\Data")
getwd()
 
defaultwd = getwd()
defaultwd
 
# -- 경로/파일 정의
inFile <- file.path(defaultwd, "AirlineDemoSmall.csv")
inFile                                                          # C:/RProject/MRS/Data/AirlineDemoSmall.csv
 
# -- 파일 가져오기
Dataset1 <- rxImport(inData = inFile, outFile = "C:\\RProject\\MRS\\Data\\AirlineDemoSmall.xdf",
                    stringsAsFactors = TRUE, missingValueString = "M", rowsPerRead = 200000,
                    overwrite = TRUE)
 
# -- 가져온 파일 확인
head(Dataset1)
 
# -- 사용자 경로/파일 설정
inFile2 <- file.path("C:\\RProject\\MRS\\Data""iris_data.csv")
inFile2
 
# -- 파일 가져오기
Dataset2 <- rxImport(inData = inFile2, outFile = "C:\\RProject\\MRS\\Data\\info.xdf",
                    stringsAsFactors = TRUE, missingValueString = "M", rowsPerRead = 200000,
                    overwrite = TRUE)
 
head(Dataset2)
 
# ------------------------------
# -- 여러 CSV 파일 가져오기 
#     샘플 다운로드: http://packages.revolutionanalytics.com/datasets/
# ------------------------------
append <- "none"
for (i in 2000:2009)
{
    importFile <- paste("C:\\RProject\\MRS\\Data", i, ".csv", sep="")
    #cat(importFile)
    
    mortDS <- rxImport(importFile, "C:\\RProject\\MRS\\Data", append=append)
    append <- "rows" # 기존 DS에 추가
}
 
nrow(mortDS)
 
# ------------------------------
# -- CSV 쓰기
# ------------------------------
 
# -- 아직 모름
 
 
# ***********************************************
# -- XDF 정의
# ***********************************************
 
# ------------------------------
# -- XDF 지정
# ------------------------------
 
# -- 사용자 경로/파일 설정
inFile3 <- file.path("C:\\RProject\\MRS\\Data""iris_data.csv")
inFile3
 
outFile3 = file.path("C:\\RProject\\MRS\\Data""iris_data.xdf")
outFile3
 
# -- XDF 생성
Dataset3 <- rxImport(inData = inFile3, outFile = outFile3,
                    stringsAsFactors = TRUE, missingValueString = "M", rowsPerRead = 200000,
                    overwrite = TRUE)
 
head(Dataset3)
 
class(Dataset3)                                                 # 'RxXdfData'
str(Dataset3)                                                   # 'RxXdfData'[package "RevoScaleR"]
mode(Dataset3)                                                  # 'S4'
 
# ------------------------------
# -- XDF 읽기(RxXdfData)
# ------------------------------
 
# -- 파일 설정
iristFile <- file.path("C:\\RProject\\MRS\\Data""iris_data.xdf")
 
# -- DS 가져오기
irisDS <- RxXdfData(iristFile)
head(irisDS)

cs



-- Files

AirlineDemoSmall.zip

iris_data.csv

iris_data.xdf




반응형

+ Recent posts