How can we import data from a text file having names of the columns in first row?
Answers
Read a .csv and Explore the Arguments
Let’s start by opening a .csv file containing information on the speeds at which cars of different colors were clocked in 45 mph zones in the four-corners states (CarSpeeds.csv). We will use the built in read.csv(...) function call, which reads the data in as a data frame, and assign the data frame to a variable (using <-) so that it is stored in R’s memory. Then we will explore some of the basic arguments that can be supplied to the function.
# First, set a working directory (see lesson 'Analyzing Patient Data' for more # info) setwd('~/swc')
# Import the data and look at the first six rows carSpeeds <- read.csv(file = 'data/car-speeds.csv') head(carSpeeds)
Color Speed State 1 Blue 32 NewMexico 2 Red 45 Arizona 3 Blue 35 Colorado 4 White 34 Arizona 5 Red 25 Arizona 6 Blue 41 Arizona