Download presentation
Presentation is loading. Please wait.
Published byMarybeth Gilbert Modified over 7 years ago
1
variables numeric character string logical > a = 24 > b<-25
> sqrt(a+b) > a = "The dog ate my homework" > sub("dog","cat",a) > a = (1+1==3) > a numeric character string logical
2
variables paste("X", "Y") paste("X", "Y", sep = " + ")
paste("Fig", 1:4) paste(c("X", "Y"), 1:4, sep = "", collapse = " + ") x<-2.17 y<-as.character(x) z<-as.numeric(y) a = c(1,2,3) a*2
3
Vector/list a = c(7,5,1) a[2] doe = list(name="john",age=28,married=F)
doe$name
4
id locallization tumorsize progress 1 xx348 proximal 6.3 FALSE
id<-c("xx348", "xx234", "xx987") locallization<-c("proximal", "distal", "proximal") progress<-c(F, T, F) tumorsize<-c(6.3, 8.0, 10.0) results<-data.frame(id, locallization , tumorsize, progress) > results id locallization tumorsize progress 1 xx proximal FALSE 2 xx distal TRUE 3 xx proximal FALSE
5
id locallization tumorsize progress 1 xx348 proximal 6.3 FALSE
results[3,2] results[1,] results[c(1,3),] results[c(T,F,T),] results$locallization results[ results$locallization=="proximal", ] > results id locallization tumorsize progress 1 xx proximal FALSE 2 xx distal TRUE 3 xx proximal FALSE
6
results<-edit(results)
7
x = c(1, 1, 2, 3, 5, 8) x[c(TRUE, TRUE, FALSE, FALSE, TRUE, TRUE)] x[c(TRUE, FALSE)] x == 1 x[x == 1] x[x%%2 == 0] y = c(1, 2, 3) y[]=3 y
8
a=matrix(1:9, ncol = 3, nrow = 3)
b=matrix(c(TRUE, FALSE, TRUE), ncol = 3, nrow = 3) b x=1:10 y=11:20 z=matrix(c(x,y)) z z=matrix(c(x,y),nrow=2) z=matrix(c(x,y),nrow=4)
9
R code max(z) min(z) length(z) mean(z) sd(z) sum(z)
10
expression<-factor(c("over","under","over","unchanged","under","under"))
levels(expression) expression[6]="x" expression
11
Working directory setwd("D:/data")
x<-read.table("profiles.csv", sep=",", header=TRUE) matplot(x, type="l") matplot(x, type="l", xlab="fraction", ylab="quantity", col=1:6, lty=1:5, lwd=2) write.table(x, file="test.txt", sep="\t")
12
Apply the max function on columns (2) or rows (1) of matrix x
xmax<-apply(x, 2, max) xmax ymax<-apply(x, 1, max) ymax Apply the max function on columns (2) or rows (1) of matrix x
13
Legend matplot(x,type="l",xlab="fraction",ylab="quantity",col=1:6,lty=1:5, lwd=2) legend(x=600,legend=names(x),col=1:6,lty=1:5,lwd=2,bg="snow")
14
> x x p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 xmax <- apply(x, 2, max) xscaled = scale(x, scale=xmax, center=FALSE)
15
Writing a Program Can save a file as do in perl
Use “source” command to load the file
16
Write this to a file named “profile.r” and save to D:\data
setwd("D:/data") x<-read.table("profiles.csv", sep=",", header=TRUE) xmax <- apply(x, 2, max) xscaled = scale(x, scale=xmax, center=FALSE) matplot(xscaled, type="l") Write this to a file named “profile.r” and save to D:\data
17
Write this to a file named “profile.r” and save to D:\data
setwd("D:/data") source("profile.r") Write this to a file named “profile.r” and save to D:\data
18
Function Functions in R are dened using the function keyword. Curly braces are used to dene the body of the function. The value / object returned by the function is indicated by the return keyword.
19
square = function(x) { return(x^2) } z=square(4) z
20
plus= function(x,y) { return(x+y) } z=plus(4,5) z
21
z=10 z=sqrt(z) if (z<5) print("<5") else if (z == 5) print ("z==5") else print("z>5")
22
Conditions
23
evenodd = function(x) {
if (!is.numeric(x)) { print("neither") } else if (x%%2 == 0) { print("even") else { print("odd") evenodd(10)
24
Loop for while repeat
25
for (x in 1:3) { print(x) } for (x in c("hello", "goodbye")) {
26
for (x in 1:4) { if (x%%2==0) next print(x) } for (x in 1:3) { if (x==2) break
27
#test x = 1 while (x < 3) { print(x) x = x + 1 }
28
cars <- c(1, 3, 6, 4, 9) plot(cars) barplot(cars) suvs <- c(4,4,6,6,16) hist(suvs) pie(suvs)
29
x <- c(1,3,6,9,12) y <- c(1.5,2,7,8,15) plot(x,y) myline.fit <- lm(y ~ x) abline(myline.fit) x2 <- c(0.5, 3, 5, 8, 12) y2 <- c(0.8, 1, 2, 4, 6) points(x2, y2, pch=16, col="green")
30
d<-dist(t(xscaled),method="euclidean")
dendrogram=hclust(t(d),method="complete",members=NULL) plot(dendrogram) rect(1,0,2,1)
31
cummean = function(x){
n = length(x) y = numeric(n) z = c(1:n) y = cumsum(x) y = y/z return(y) } n = 10000 z = rnorm(n) x = seq(1,n,1) y = cummean(z) X11() plot(x,y,type= 'l',main= 'Convergence Plot')
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.