Basic skills in R: Getting data into and out of R
Importing data from a website
R is able to read data directly from a website that provide them in a proper format. For example, we have stored the MDMAdata.csv dataset on the webpage /images/uploads/theory/17066/MDMAdata.csv We can directly read the dataset with the function read.csv()
:
> webpage <- "http://uva.sowiso.nl/images/uploads/theory/17066/MDMAdata.csv" > MDMAdata <- read.csv(webpage) > str(MDMAdata) 'data.frame': 12 obs. of 2 variables: $ t: num 0.25 0.5 0.75 1 1.5 2 3 4 6 8 ... $ C: num 0 19.2 74.6 125 200 ...
Importing data directly from a website is best when the data are continously being updated. We can , for example, get the global concentration (in ppm) of atmospheric \(\mathrm{CO}_2\) over the last several decade, regularly updated, at a National Oceanic and Atmospheric Administration (NOAA) website. In the read.csv() instruction in the R session below we simply omit the first 42 lines with onformation about the data and we only use the data in the first and second column. Below, we make a simple scatter plot of the dataset
> webadress <- "https://gml.noaa.gov/webdata/ccgg/trends/co2/co2_annmean_mlo.csv" > CO2 <- read.csv(webadress, skip=42)[,1:2] > str(CO2) 'data.frame': 64 obs. of 2 variables: $ year: int 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 ... $ mean: num 316 317 318 318 319 ... > head(CO2) year mean 1 1959 315.98 2 1960 316.91 3 1961 317.64 4 1962 318.45 5 1963 318.99 6 1964 319.62
> with(CO2, plot(x = year, y = mean))