R Basics

October 7, 2009
by Jeremy Koelmel (jpk07)

R basics
October 7, 2009
by Jeremy Koelmel (jpk07)

R Basics, Here is how to:
1) to manually set up data in R
2) Import data from outside R (as .txt)
3) export data out of R (as .txt)
4) To export and import R code to run in R

# writing simple exponential data in R
#assign arrays
X <- c(1:10);
Y <- c(X^2);
#matrix
XYdata <- cbind(X,Y);
#transpose data
XYdata <- t(XYdata);
#select one
XYdata[,7];
# Plot
plot(X, Y, main=”Exponential Function”);

#exporting
write.table(XYdata, file=”Desktop/XYdata.txt”, sep=”,”);
#getting the working directory
getwd();
#setting the working directory
setwd(”~/Desktop”);

#Importing
RoseData <- read.table(file=”RoseData.txt”);
#see all plots
plot(RoseData, main=”Multiflora Rose Distribution in a Pine Stand”);
#comparing two variables
MFRdensity <- RoseData[,3];
DistanceFromEdge <- RoseData[,9];
plot(DistanceFromEdge,MFRdensity);
#lm(DistanceFromEdge,MFRdensity); not working because more than one instance of y for single x
# Writing .R function: Write in a simple text format (.txt) and Save as “blahblah”.R

# ie. in this format
X <- c(1:10);
Y <- c(X^2);
XYdata <- cbind(X,Y);
plot(X, Y, main=”Exponential Function”);

# get to the right directory
setwd(”Dir”);
# enact it in R
source(”Graph.R”);

Love Jeremy

PS: Further Resources:

http://www.math.ilstu.edu/dhkim/Rstuff/Rtutor.html

http://www.cyclismo.org/tutorial/R/



Leave a Reply

You must be logged in to post a comment.