Saturday, December 21, 2013

1.3 Importing the data set in the R Console

Importing data in R

Verify if the correct Working Directory is set. Also make sure that the data files are placed in this folder. 
If these two operations are executed, you are ready to import data files into the R console.

Importing the csv file


Figure 6: Imorting and making a matrix of the data set in the r console
Figure 6: Importing and making a matrix of the data file


You can select the file you wat to import into the R console. This can be executed in the following way:
  1. Importing the data file: Type in the command read.csv('*name of your file*'). In the example of Figure 6 the file Flowersales.csv is imported. In this example the code read.csv('Flowersales.csv') is used. After pressing Enter information about the data set appears in the R console. Please note that the code that is inserted in the R console is case sensitive, "flowersales.csv" could not be found. Please also not that the name of the file has to be put between 'quotation marks'.
  2. Creating a variable for the data file:  The next step is creating variables for the data file. In the introduction of this tutorial it is explained how to create a variable with the <- code. For the data file Flowersales.csv a variable is created. This is done by using the following code: Flowersales<-read.csv('Flowersales.csv'). The file Flowersales.csv is no longer seen as an external file, but as a variable in this session. This makes analyzing the data set in the R console way easier. The purpose of making a variable is that the code for importing the data file [read.csv('Bloemenverkoop.csv')] does not have to be insterted every time. Now that a variable is created for this data file, you only have to type in Flowersales to show information about the data file.
  3. Making a matrix of the data file: By making a matrix of the just created variable Flowersales, the R console could read the data in a better way. Making a matrix in the R console is easy by using the command: attach(*Name of the variable of the data file*). Figure 6 shows that the command attach(Bloemenverkoop) is used (Bloemenverkoop is the Dutch word for Flowersales). Now catagories of the data set could easy be shown in the R consol. For example by showing information about the catagory Tulips, you easily use the command Tulips. In the next chapter you will see that in front of the command Tulips differerent functions could be placed to analyze this catagory.

Remarks

  • Like chapter 1.1 explains about the Working Directory; The Working Directory is a map in which R searches for data files. So, the data files you want to analyze have to be placed in this map to be found by R.
  • It is not neccesary to name the variable Flowersales, you could decide yourself how you name the variable. With the <- code R recognized the data file. Flowers<-read.csv('Flowersales.csv') is for example acceptable as well. In this case the variable Flowers has to be used during the whole analyzing process in the R console.

No comments:

Post a Comment