Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The most difficult part of the learning curve in R is often getting going – many datasets are pre-installed in the packages and organised, so it is difficult to see how you to import your own data into R. This post takes you step by step through the process of making a table from a spreadsheet and then a simple graph.
The first thing is to get some data. A .csv file is a common “spreadsheet” like file. Currently I’m working with some air quality data downloaded from the UK air quality archive. The data I’ve downloaded is of 2009 data from Nottingham, UK containing automated measurements of Nitric Oxide, NO2, Ozone, and Sulphur Dioxide. The file is here. You can cut and paste the code below into R.
The first thing to do is put the data into a variable, called data. Copy the spreadsheet file into your working directory. We then use the read.csv for this:
columns <- c("date", "time", "NO", "NO_status", "NO_unit", "NO2", "NO2_status", "NO2_unit", "ozone", "ozone_status", "ozone_unit", "SO2", "SO2_status", "SO2_unit") data <- read.csv("27899712853.csv", header = FALSE, skip = 7, col.names = columns, stringsAsFactors = FALSE)
data[1:10,]
data$NO[5:10]
plot(data$NO)
## start by saving the original graphical parameters def.par <- par(no.readonly = TRUE) x <- data$NO y <- data$ozone xlabel <- "NO" ylabel <- "ozone" layout(matrix(c(2,1,1,3,1,1), 2, 3, byrow = TRUE)) plot(x, y, xlab = xlabel, ylab = ylabel, pch = 20) plot(x, xlab = NA, ylab = xlabel, pch = 20) plot(y, xlab = NA, ylab = ylabel, pch = 20) ## reset the graphics display to default par(def.par)
matrix(c(2,1,1,3,1,1), 2, 3, byrow = TRUE)
Tagged: R, statistics
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.