How do I import my data into R for statistical analysis?
First, you need to understand your data format. This makes it easier to translate it into R with ease. R can read files in different formats such as Excel, SAS, SPSS and for more advance use of R, access SQLtype database.
If you have a dataset in Microsoft Excel spreadsheets and you want to import this into R. Take the following steps:
Step 1: When you download and install R from the central repository, CRAN (Comprehensive R Archive Network); you need to choose a mirror site among the series of mirror sites around the World. Choose a mirror site closest to you to avoid overloading the main CRAN site.
Step 2: Install the package you want to work with e.g. I want to carry out Latent Class Modelling so I installed the package poLCA.
Step 3: R uses the working directory for reading and writing in files. The Excel spreadsheet you want to import into R is in a location on your computer – this is your working directory. You need to specify this working directory by going to file and selecting change working directory:
To find this directory, the command getwd() (get working directory) can be used:
Step 4: Specify which sheet you want by typing the command dir(). This command will show all the files in the working directory.
To analyse the sheet called “fulldatareg.csv”, I then need to command R to read the sheet using the function read.csv which creates a data frame. This is the main way to read data in tabular form. For instance, I have a data file named fulldatareg.csv the command:
> analysis <- read.csv (“fulldatareg.csv”, header = TRUE) will create a data frame named analysis. The header can be FALSE or TRUE but in this case TRUE – indicating that the ﬁle contains the names of the variables on its ﬁrst line. To display the data frame, use the command: read.csv(“fulldatareg.csv”)
Categories: R programming