R FAQs
Q:
I’m working through the R tutorial- am up to the stage where I have copied and pasted the data into notepad (text editor) and am trying to get the program to read the table. The problem is that it just keeps on coming up in blue that it can’t find the data. I have tried saving in various places but no luck, and when I type TABLE into the console it comes up as Error: object ‘TABLE’ not found. Do you have any suggestions?
A1:
You need to specify the path to the file location in the command from top of page 2:
TABLE <- read.table("PATH_TO_YOUR_DATA",skip=2)
in the example from the tutorial I had put the data file (Radiation_2009181.txt) in a directory called R_tutorial under C:
this is why in my case the command read as:
TABLE <- read.table("C:/R_tutorial/Radiation_2009181.txt",skip=2)
you need to replace "C:/R_tutorial/Radiation_2009181.txt" by the correct location on your machine.
Also note that the "/" are oposite to the ones actually used by windows ("\"), and it is often a source of error when specifying a path.
You will then have loaded the data in R and can scan it by typing TABLE.
A2:
The computers in the lab seem to be set to automatically add an extension to the file names but they hide it.
So for instance when you open a text file and give it the name Radiation_2009181 it will automatically be known as Radiation_2009181.txt.
But if you name it Radiation_2009181.txt it will actually be known as Radiation_2009181.txt.txt
And when you ask R to look for Radiation_2009181.txt it does not find it...
So the idea is to save your dataset without any .txt extension but call it from R with the .txt extension.
Note this setting can be changed: got to Tools/Folder Options/View and then untick the box "Hide extensions for know file types"
-------------------------------------------------
Q:
I am trying to plot my data for my Monitoring Environmental Change coursework 1 using R, the first set of measurements I plot are fine but when I try and add the 2nd and 3rd sets of measurements to the sam graph it keeps plotting lines even though I type points, it looks terrible, do you have any suggestions about where I am going wrong?
A1:
The command I used in the Tutorials was plotting both lines and symbols because it had the type="b" argument (b for both).
if you substitute it with type="p" (p for points) it will plot only the symbols.
By default symbols are set to empty circles but you can change using the pch argument.
For instance if you only want points in the command from the Tutorial 1 you could type:
lines(DECTIME,KUP,type="p",pch=1,col="red")
The alternative is to use the points command instead, with no type= argument:
points(DECTIME,KUP,pch=1,col="red")
-------------------------------------------------
Q:
Is it possible to get R to put a trend line on the graph and tell me what its equation is and the r2 value like you can in Excel? If so, please could you tell me what the commands are?
A1: you can carry out a linear regression with R using the lm command (linear model fitting)
Suppose you want to perform a regression of vector Y over vector X, you would first create the fitted model using lm:
fit <- lm(Y ~ X)
you can then ask R to print relevant information relative to the fitted relation:
print(fit)
and then store the coefficients you want (here the slope and intercepts of the regresion line):
slope<-coef(fit)[2]
intercept<-coef(fit)[1]
you can finally plot your data and add a regression line:
plot(X,Y)
abline(intercept, slope,col="red")
A2: Another interesting function to tderive trend is the lowess.
It performs a so called locally weighted polynomial regression and therefore goes beyond the linear trends you could identify using lm.
see ?lowess for more details
A3: The coefficient of determination (R) can be conputed using the correlation command cor:
Coef_Det <- cor(X,Y)
in the same way you can check variances and covariances using the functions var and cov.
-------------------------------------------------
Q: How can I plot contours of hydraulic data with R?
My data is organized as a table with the position (X and Y) in the first 2 columns and the hydraulic data (Z) in the third.
A1:
There is a function in R called levelplot which should plot contours on
an (x,y) plane. The description of the function is on the R refcard at
the bottom of page 3 (see webpage under section operation/programming
guides/: R refcard). You might have to install and load the package
"lattice" first to use this function. You can then get a complete
descripion when typing ?levelplot.
The syntaxis is:
levelplot(z~x*y)
where z is your measured value and x and y are its coordinates.
You can also plot 3d surface with the same package and the function wireframe:
wireframe(z~x*y)