Reading Data for Travel Analysis into R

Reading Data for Travel Analysis into R

The last post covered all of the basics on how to prepare to start an analysis in R. Next, we need some data to analyze. Thanks to the internet we can find data on just about anything, including the historical trends of weather in almost any location. In this case we’re going to use data from Weather Underground. They provide a number of variables, but the raw format would be difficult to use:

MST,Max TemperatureF,Mean TemperatureF,Min TemperatureF,Max Dew PointF,MeanDew PointF,Min DewpointF,Max Humidity, Mean Humidity, Min Humidity, Max Sea Level PressureIn, Mean Sea Level PressureIn, Min Sea Level PressureIn, Max VisibilityMiles, Mean VisibilityMiles, Min VisibilityMiles, Max Wind SpeedMPH, Mean Wind SpeedMPH, Max Gust SpeedMPH,PrecipitationIn, CloudCover, Events, WindDirDegrees

Fortunately, the “XLSX” package we loaded in the last post has some tools to help us format the information. First, copy and paste the data above into an excel file. Then we will write a command in R that will read the table into the program so you can do further analysis on it. The code below is defining an object named “temps” which is equal to the data on the sheet of the Sedona_temps.xlsx file that is labeled “Sedona_temps”. Finally, it is also telling the program that the data has headers. If that was set to false, the variable names would pull into the first row of data instead of being the column header.

temps<-read.xlsx("Sedona_temps.xlsx", sheetName="Sedona_temps", header= TRUE)

Now, in the upper right hand corner of R Studio you should see a file named “temps” alongside a description of the data table that tells you the number of variables and rows.

This is just one of many ways to read data into R. Another option would be to save the data as a .txt file and use this command to read the file:

temps<- read.table(file="Sedona_Temp_Data.txt", header=TRUE, sep=",", quote="")

If you want to see what the data looks like, simply type this command to open a new tab that shows the raw data:


Next up we’re going to work on making sense of this data with some of R’s analytic tools.

Leave a Reply

Your email address will not be published. Required fields are marked *