Introduction & read.csv
1. Introduction & read.csv
Hi, and welcome to the first importing data in R course.2. Importing data in R
Imagine this situation: A colleague of yours is still doing his or her analyses in Excel and finally decided to transition to R. He or she needs an easy way to convert the Excel spreadsheets into R data frames,3. Importing data in R
but he or she can't seem to find the tools to do so. Well, getting to know these tools is exactly what we'll do here.4. 5 types
In this two-part course, we will focus on 5 types of data:5. 5 types
data from flat files,6. 5 types
data from Excel,7. 5 types
data from databases,8. 5 types
data from the web,9. 5 types
and finally data from other statistical software like SAS, SPSS, and Stata.10. 5 types
You'll learn to convert each data format, one after the other, into an R data frame, ready to do some fancy analyses.11. Flat files
Let's start off with flat files. Flat files are typically simple text files that display data as tables. Have a look at this example, states-dot-csv, a flat file where CSV stands for comma-separated values. The data lists basic information on some US states. The first line here gives the names of the different columns or fields. After that, each line is a record, and the fields are separated by a comma, hence the extension CSV. For example, there's the state Hawaii with the capital Honolulu and a total population of 1-point-42 million. What would this data look like in R? Well, actually, the structure nicely corresponds to a data frame, that ideally looks like this: the rows in the data frame correspond to the records and the columns of the data frame correspond to the fields. The field names are used to name the columns of the data frame. But how to go from this CSV to this data frame? We're in luck, because the standard distribution of R provides functionality to import these flat files into R as a data frame.12. utils - read.csv
These functions belong to the utils package that is loaded by default when you start R. More specifically, we'll need the read.csv() function, as follows. The first argument of read.csv() is the path to the file you want to import in R. If the file is in your current working directory, simply passing the filename as a character string works. If your file is located somewhere else, things get tricky. Depending on the platform you're working on, Linux, Microsoft, Mac, whatever, file paths are specified differently. To build a path to a file in a platform-independent way, you can use the file-dot-path function. Suppose our states-dot-csv file is located in the datasets folder of the home directory. You can use file-dot-path, like this. Because I'm working on a Mac, this is the resulting path. I can now use this path inside read-dot-csv to point to the correct file, like this.13. Let's practice!
Alright, that's enough to get you started for now. Time to give importing data in R a first try in the exercises!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.