官术网_书友最值得收藏!

Generating R datasets

Here, we show you how to generate an R dataset called iris.RData by using the R save() function:

path<-"http://archive.ics.uci.edu/ml/machine-learning-databases/" 
dataSet<-"iris/bezdekIris.data" 
a<-paste(path,dataSet,sep='') 
.iris<-read.csv(a,header=F) 
colnames(.iris)<-c("sepalLength","sepalWidth","petalLength","petalWidth","Class") 
save(iris,file="c:/temp/iris.RData") 

To upload the function, we use the load() function:

>load("c:/temp/iris.RData") 
> head(.iris) 
  sepalLength sepalWidth petalLength petalWidth       Class 
1         5.1        3.5         1.4        0.2 Iris-setosa 
2         4.9        3.0         1.4        0.2 Iris-setosa 
3         4.7        3.2         1.3        0.2 Iris-setosa 
4         4.6        3.1         1.5        0.2 Iris-setosa 
5         5.0        3.6         1.4        0.2 Iris-setosa 
6         5.4        3.9         1.7        0.4 Iris-setosa 

Note that the extension of .RData is not critical. The second way to save R data is to apply an R function called saveRDS(), shown in the code here:

inFile<-"http://canisius.edu/~yany/data/ff3monthly.csv" 
ff3monthly<-read.csv(inFile,skip=3) saveRDS(ff3monthly,file="c:/temp/ff3monthly.rds")

The corresponding function to load the dataset is called readRDS(). Another important property when using the rds dataset is that we can assign another more convenient name, shown in the code that follows. In this case, we call it abc instead of ff3monthly:

>abc<-readRDS("c:/temp/ff3monthly.rds") 
>head(abc,3) 
        DATE MKT_RF     SMB     HML     RF 
1 1926-07-01 0.0296 -0.0230 -0.0287 0.0022 
2 1926-08-01 0.0264 -0.0140  0.0419 0.0025 
3 1926-09-01 0.0036 -0.0132  0.0001 0.0023 
>head(ff3monthly,3) 
        DATE MKT_RF     SMB     HML     RF 
1 1926-07-01 0.0296 -0.0230 -0.0287 0.0022 
2 1926-08-01 0.0264 -0.0140  0.0419 0.0025 
3 1926-09-01 0.0036 -0.0132  0.0001 0.0023 
主站蜘蛛池模板: 家居| 贵定县| 沅江市| 烟台市| 手游| 沧州市| 阿鲁科尔沁旗| 五家渠市| 济宁市| 辰溪县| 甘洛县| 大埔县| 宣汉县| 年辖:市辖区| 通渭县| 英超| 霍州市| 财经| 南城县| 塘沽区| 古浪县| 濮阳县| 房山区| 兴隆县| 内江市| 额敏县| 淮北市| 囊谦县| 信丰县| 泽库县| 巴彦县| 青田县| 古田县| 巨野县| 奉新县| 嘉鱼县| 白河县| 乌兰浩特市| 天镇县| 邹平县| 修水县|