Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##p1---------------------------------------------------------
- #clear memory
- rm(list=ls())
- #list defined variabls
- ls()
- #create/load data
- data <- c(1, 2, 3, 4)
- data2 <- c(2, 4, 8, 16)
- #check data
- ls()
- data
- data2
- #plot graph
- plot(data, data2, xlab = "something", ylab = "something else")
- #find working directory
- getwd()
- #Set working directory (to access files for R)
- setwd("\\\\folder")
- #read data file
- filename<-read.csv
- #help command
- help(read.csv)
- #access each individual column of a data set using "name"$header
- datasetname$trial
- #find column names for data set
- names(dataset)
- #see variables of a data set
- attributes(name)
- #read csv command to read and name a set of data
- setname <- read.csv(file="file name",head=TRUE,sep=",")
- #store number as a variable
- letter<-number
- #initialize a list of numbers the numeric command can be used
- a<-numeric(10)
- #String: a string is specified by using quotes
- a<-"hello"
- #find type of string using
- typeof(string name)
- eg: typeof(a)
- #look at file to check several variables
- summary(filename$variable)
- #creating a dataframe
- #here we create several separate vectors
- > a <- c(1,2,3,4)
- > b <- c(2,4,6,8)
- > levels <- factor(c("A","B","A","B"))
- #we then create a data frame using these vectors
- > bubba <- data.frame(first=a,second=b,f=levels)
- #variable types. 2 variables= TRUE and FALSE
- a=TRUE
- typeof(a)
- b=FALSE
- typeof(b)
- #creating a vector and make it into a table
- > a <- factor(c("A","A","B","A","B","B","C","A","C"))
- > results <- table(a)
- #check it
- > results
- > attributes(results)
- > summary(results)
- #creating two way tables, a=x b=y collums (values are examples)
- > a <- c("Sometimes","Sometimes","Never","Always","Always","Sometimes","Sometimes","Never")
- > b <- c("Maybe","Maybe","Yes","Maybe","Maybe","No","Yes","No")
- > results <- table(a,b)
- > results
- ##p2-----------------------------------------------------------------
- #summary data set
- summary(datasetname)
- #boxplot
- boxplot(quantileofdata[eg.ppm], col="")
- #abline (add a line annotation)
- abline(h=value of Y to anotate) eg:(h=12)
- #create dashed line
- lty=value
- #histogram
- hist(quantiledata[eg. ppm), col="")
- #show data points on graph
- rug(quantiledata)
- #increase data buckets on graph
- graphcommand(breaks="[100]"
- #add line to graph
- abline(v=Y value intended, lwd[line width]value[2]
- #create table from data in r
- name <- table(quantile$quantile)
- #histogram
- hist(quantile/data)
- #scatter plot
- with(data, plot(quantile,quantile))
- #colour quantile data in plot
- col=quantile$quantile
- #set up plot window for 2 graphs
- par(mfrow=c(1,2), mar=c(5,4,2,1))
- #create variable
- variable name <-subset(data, region=="quantile")
- #make q plot
- qplot(displ, hwy, data=dataname)
- #add gray areas and trndlines
- qplot(displ, hwy, data=dataname, geom=c"point", "smooth"))
- #allpurpose qplot
- qplot=drv,hwy,data=dataname,geom="boxplot")
- #assign boxplots by variable
- qplot(drv,hwy,data=mpg,geom="boxplot",color=manufacturer)
- #histogram display freq for each variable
- qplot(hwy, data = dataname, fill =drv)
- #scatter plot with 3 facets
- qplot(hwy, data = dataname, fill =drv)
- #histogram with 3 facets
- qplot(hwy, data = mpg, facets = drv ~ ., binwidth = 2)
- #lattice to creat object assigned with variable (variable)
- variable <- ggplot(mpg, aes(displ,hwy))
- #add layer to variable
- variable(geom_point()
- #add smooth line to table
- +geom_smooth()
- #change line to linear model(lm)
- +geom_smooth(method="lm)
- #add colour legend to graph
- g+geom_point(aes(color=drv), size = 4, alpha = 1/2)
- #ggplot first layer
- ggplot(data=pharynx,aes(x=day, y=pumps))
- #gplot 2nd layer
- ggplot(pharynx, aes(x=as.factor(day), y=pumps)) + geom_boxplot(fill="red", alpha=0.2) + xlab("day")
- #qplot boxplot 1
- qplot(x=day, y=pumps,color=treatment,data=pharynx,geom="boxplot")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement