Page 54 - Applied Statistics Using SPSS, STATISTICA, MATLAB and R
P. 54
2.1 Preliminaries 33
» meteo=[
181 143 36 39 37 % Pasting starts here
114 132 35 39 36
101 125 36 40 38
...
14 70 35 37 39 % and ends here.
]; % Typed after the pasting.
One would then proceed to save the meteo matrix with the save command. In
order to save the data file (as well as other files) in a specific directory, it is
advisable to change the directory with the cd command. For instance, imagine one
wanted to save the data in a file named Meteodata , residing in the
c:\experiments directory. One would then specify:
» cd(‘c:\experiments’);
» save Meteodata meteo;
The MATLAB dir command would then list the presence of the MATLAB file
Meteodata.mat in that directory.
In a later session the user can retrieve the matrix variable meteo by simply
using the d loa command: » load Meteodata .
This will load the meteo matrix from the Meteoda ta.mat file as can be
confirmed by displaying its contents with: » meteo .
2.1.1.4 R Data Entry
The tabular form of data in R is called data frame. A data frame is an aggregate of
column vectors, corresponding to the variables related across the same objects
(cases). In addition it has a unique set of row names. One can create an R data
frame from a text file (direct data entry from an EXCEL file is not available). Let
us illustrate the whole procedure using the meteo.xls file shown in Figure 2.1
as an example. The first thing to do is to convert the numeric data area of
meteo.xls to a tab-delimited text file, e:meteo. txt , say, from within
EXCEL (with Save As ). We now issue the following command in the R console:
> meteo <- read.table(file(“e:meteo.txt”))
The argument of file is the path to the file we want to read in. As a result of
read.table a data frame is created with the same numeric information as the
meteo.xls file. We can see this with:
> meteo
V1 V2 V3 V4 V5
1 181 143 36 39 37
2 114 132 35 39 36
3 101 125 36 40 38
...
For future use we may now proceed to save this data frame in e:meteo , say,
with save(meteo,file=“e:meteo”). At a later session we can immediately
load in the data frame with load(“e:meteo”).