getwd() # get work directory setwd('./') # set work directory
ls() # Display current objects objects() # Display current objects rm(x) # Remove object x from R work space rm(list = ls()); # remove all environment objects install.packages("mmass") #Install package mmass
x + y # 더하기 addition x - y # 빼기 subtraction x * y # 곱하기 multiplication x / y # 나누기 division x ^ y # 자승 exponentiation x %% y # 나머지 modular arithmetic x %/% y # 정수 나누기 integer division x %*% y # 행렬곱 Objective matrix multiply
x == y # 같은가 test for equality x < y # 보다 작은가 Less than x > y # 보다 큰가 Greater than x <= y # 보다 작거나 같은가 test for less-than-or-equal x >= y # 보다 크거나 같은가 test for greater-than-or-equal x && y # 참이고 참인가 boolean and for scalars x || y # 참이거나 참인가 boolean or for scalars x & y # 참이고 참인가 boolean and for vectors (vector x,y,result) x | y # 참이거나 참인가 boolean or for vectors (vector x,y,result) !x # 참이 아닌가 boolean negation
> x <- c(1,2,4) [1] 1 2 4 > y <- c(x,x,8) [1] 1 2 4 1 2 4 8 > y[3] [1] 4 > mean(y) [1] 3.142857 > sd(y) > z <- mean(y) > z [1] 3.142857
if (r == 4) { x <- 1 } else { x <- 3 }
if(client=='private'){ x <- 1 } else if(client=='public'){ x <- 3 } else { x <- 5 }
> x <- 1:10 > y <- ifelse(x %% 2 == 0,5,12) > y [1] 12 5 12 5 12 5 12 5 12 5 > x <- c(5,2,9,12) > ifelse(x > 6,2*x,3*x) [1] 15 6 18 24
nreps <- 10 for (i in 1:nreps) { print(i) }
> x <- c(5,12,13) > for (n in x) print(nˆ2) [1] 25 [1] 144 [1] 169
> for (fn in c("x","y")) print(fn)
i <- 20 while(i>10) { i <- i-1 print(i) }
i <- 1 while(1) { i <- i+4 if (i > 10) break } > i [1] 13
f <- function(x) { return(x+1) } oddcount <- function(x) { k <- 0 for (n in x) { if (n %% 2 == 1) k <- k+1 } return(k) } > oddcount(c(1,3,5)) [1] 3 > oddcount(c(1,2,3,7,9)) [1] 4
addone <- function(x) return(x+1) gaddone <- function() w <<- w+1 # use of superassignment op # superassignment는 전역변수 할당에 사용된다.
f <- function(x,y=2){ return(x+y) } > f(3) [1] 5
> g <- function(h,a,b) h(a,b) > g(function(x,y) return(x*y),2,3) [1] 6
> u <- 1 > v <- 8 g <- function(x) { x <- x + 1 u <- u + x return(u) } > g(v) [1] 10 > u [1] 1 > v [1] 8
<<- : Global Variable definition
two <- function(u) { x <- 2*u y <<- 2*u z <- 2*u } > x <- 1 > y <- 2 > z <- 3 > two(1) > x [1] 1 > y [1] 4 > z [1] 3
Concatenate : c
> x <- c(1,2,4) > length(x) # length of x [1] 3 > y[1] <- 5 # 첫번째 요소에 5할당 > y[2] <- 12 # 두번째 요소에 12할당 > y <- c(5,12) # 위의 결과와 같다
Colon : ":"
> 5:8
[1] 5 6 7 8
> 5:1
[1] 5 4 3 2 1
> i <- 2 > 1:i-1 [1] 0 1 > 1:(i-1) [1] 1
seq() : sequence
> seq(5,8) [1] 5 6 7 8 > seq(12,30,3) [1] 12 15 18 21 24 27 30 > seq(1.1,2,length=10) [1] 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0
rep() : repeat
> x <- rep(8,4) > x [1] 8 8 8 8 > rep(1:3,2) [1] 1 2 3 1 2 3
Arithmatic
> x <- c(1,2,4) > x+x # addition [1] 1 4 6 > x*x # multiplication [1] 1 4 16 > x/x # dividing [1] 1 1 1
any() :
> x <- 1:10 > if (any(x > 8)) print("yes") [1] "yes" > if (any(x > 88)) print("yes")
all() :
> if (all(x > 88)) print("yes") > if (all(x > 0)) print("yes") [1] "yes"
연산에서 범위가 벗어나면 처음 요소부터 연산에 활용한다.
> c(1,2,4) + c(6,0,9,20,22) [1] 7 2 13 21 24 Warning message: longer object length is not a multiple of shorter object length in: c(1, 2, 4) + c(6, 0, 9, 20, 22) > x [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > x+c(1,2) [,1] [,2] [1,] 2 6 [2,] 4 6 [3,] 4 8
> y <- c(1.2,3.9,0.4,0.12) > y[c(1,3)] [1] 1.2 0.4 > y[2:3] [1] 3.9 0.4 > z <- c(5,12,13) > z[-1] # exclude element 1 [1] 12 13 > z[-1:-2] [1] 13 > z <- c(5,12,13) > z[1:length(z)-1] # 마지막을 제외하고 선택 [1] 5 12 > x <- c(12,15,8,11,24) > y <- x[-1] - x[-length(x)] # c(15, 8, 11, 24) - c(12, 15, 8, 11) > y [1] 3 -7 3 13
names() :
> x <- c(1,2,4) > names(x) NULL > names(x) <- c("a","b","ab") # assign name > names(x) [1] "a" "b" "ab" > x a b ab 1 2 4 > names(x) <- NULL # remove the names from a vector by assigning NULL: > x [1] 1 2 4 > x <- c(1,2,4) > names(x) <- c("a","b","ab") > x["b"] b 2
> u <- c(5,2,8) > v <- c(1,3,9) > u+v [1] 6 5 17 > u > v [1] TRUE FALSE FALSE > w <- function(x) return(x+1) > w(u) [1] 6 3 9 > f <- function(x,c) return((x+c)^2) # auxiliary arguments > f(1:3, 0) [1] 1 4 9 > f(1:3, 1) [1] 4 9 16 > sqrt(1:9) [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 [9] 3.000000 > y <- c(12, 5, 13) > '+'(y,4) [1] 16 9 17
> z <- c(5,2,-3,8) > w <- z[z*z > 8] > w [1] 5 -3 8 > z [1] 5 2 -3 8 > z*z > 8 [1] TRUE FALSE TRUE TRUE > z[c(TRUE,FALSE,TRUE,TRUE)] [1] 5 -3 8
which() :
> which(z*z > 8) [1] 1 3 4 nviol <- function(x) { diff <- x[-1]-x[1:(length(x)-1)] return(length(which(diff < 0))) } > x <- c(1,3,8,2) > x[x > 3] <- 0 > x [1] 1 3 0 2
ifelse() :
> x <- 1:10 > y <- ifelse(x %% 2 == 0,5,12) > y [1] 12 5 12 5 12 5 12 5 12 5 > x <- c(5,2,9,12) > ifelse(x > 6,2*x,3*x) [1] 15 6 18 24
> x <- c(12,5,13,16,8) > x <- c(x,20) # append 20 > x [1] 12 5 13 16 8 20 > x <- c(x[1:3],20,x[4:6])# insert 20 > x [1] 12 5 13 20 16 8 20 > x <- x[-2:-4] # delete element 2 through 4 > x [1] 12 20 16 8
> x <- array(1:9) > x [1] 1 2 3 4 5 6 7 8 9 > x <- array(1:9,c(3,3)) > x [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 > x <- 1:64 > dim(x) <- c(2,4,8) #dim() converts the vector into array > is.array(x) [1] TRUE > x , , 1 [,1] [,2] [,3] [,4] [1,] 1 3 5 7 [2,] 2 4 6 8 , , 2 [,1] [,2] [,3] [,4] [1,] 9 11 13 15 [2,] 10 12 14 16 , , 3 [,1] [,2] [,3] [,4] [1,] 17 19 21 23 [2,] 18 20 22 24 , , 4 [,1] [,2] [,3] [,4] [1,] 25 27 29 31 [2,] 26 28 30 32 , , 5 [,1] [,2] [,3] [,4] [1,] 33 35 37 39 [2,] 34 36 38 40 , , 6 [,1] [,2] [,3] [,4] [1,] 41 43 45 47 [2,] 42 44 46 48 , , 7 [,1] [,2] [,3] [,4] [1,] 49 51 53 55 [2,] 50 52 54 56 , , 8 [,1] [,2] [,3] [,4] [1,] 57 59 61 63 [2,] 58 60 62 64 > x[1,,] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 1 9 17 25 33 41 49 57 [2,] 3 11 19 27 35 43 51 59 [3,] 5 13 21 29 37 45 53 61 [4,] 7 15 23 31 39 47 55 63 > x[1,2,] [1] 3 11 19 27 35 43 51 59 > x[1,2,1] [1] 3
> y <- matrix(c(1,2,3,4),nrow=2,ncol=2) > y [,1] [,2] [1,] 1 3 [2,] 2 4 > m <- matrix(c(1,2,3,4,5,6),nrow=2,byrow=T) # equal to : m <- matrix(c(1,2,3,4,5,6),nrow=2) : > m [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 # ‘T’ is an abbreviation for “TRUE”.
> y %*% y # ordinary matrix multiplication 형렬곱 [,1] [,2] [1,] 7 15 [2,] 10 22 > y+y [,1] [,2] [1,] 2 6 [2,] 4 8
> y[,2] [1] 3 4 > y <- matrix(nrow=2,ncol=2) > y[1,1] = 1 > y[2,1] = 2 > y[1,2] = 3 > y[2,2] = 4 > y [,1] [,2] [1,] 1 3 [2,] 2 4 > z [,1] [,2] [,3] [1,] 1 1 1 [2,] 2 1 0 [3,] 3 0 1 [4,] 4 0 0 > z[,c(2,3)] [,1] [,2] [1,] 1 1 [2,] 1 0 [3,] 0 1 [4,] 0 0 > y <- matrix(c(11,21,31,12,22,32),nrow=3,ncol=2) > y [,1] [,2] [1,] 11 12 [2,] 21 22 [3,] 31 32 > y[2:3,] [,1] [,2] [1,] 21 22 [2,] 31 32 > y[2:3,2] [1] 22 32 > y [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > y[2:3,] <- matrix(c(1,1,8,12),nrow=2) > y [,1] [,2] [1,] 1 4 [2,] 1 8 [3,] 1 12 > x <- matrix(nrow=3,ncol=3) > x[2:3,2:3] <- cbind(4:5,2:3) # copy > x [,1] [,2] [,3] [1,] NA NA NA [2,] NA 4 2 [3,] NA 5 3
> z <- matrix(c(1,2,3,4),nrow=2) > z [,1] [,2] [1,] 1 3 [2,] 2 4 > colnames(z) NULL > colnames(z) <- c("a","b") > z a b [1,] 1 3 [2,] 2 4 > colnames(z) [1] "a" "b" > z[,"a"] [1] 1 2
> z <- matrix(1:8,nrow=4) > z [,1] [,2] [1,] 1 5 [2,] 2 6 [3,] 3 7 [4,] 4 8 > r <-z[2,] > r [1] 2 6 > attributes(z) $dim [1] 4 2 > attributes(r) NULL
suppress dimension reducting using drop
> r <- z[2,, drop=F] # Suppress dimension reduction, with the drop argument > r [,1] [,2] [1,] 2 6 > dim(r) [1] 1 2
suppress dimension reducting using as.matrix
> u <-c(1,2,3) > v <-as.matrix(u) > v [,1] [1,] 1 [2,] 2 [3,] 3
cbind() : adding
> one <- c(1,1,1,1) > z <- matrix(c(1,2,3,4,1,1,0,0,1,0,1,0),nrow=4) > z [,1] [,2] [,3] [1,] 1 1 1 [2,] 2 1 0 [3,] 3 0 1 [4,] 4 0 0 > cbind(one, z) one [1,] 1 1 1 1 [2,] 1 2 1 0 [3,] 1 3 0 1 [4,] 1 4 0 0 > q <- cbind(c(1,2),c(3,4)) > q [,1] [,2] [1,] 1 3 [2,] 2 4
rbind() : adding
> p <- rbind(c(1,2,c(3,4))) > p [,1] [,2] [,3] [,4] [1,] 1 2 3 4
deleting row
> m <- matrix(1:6,nrow=3) > m [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > m <- m[c(1,3),] # deleting second row > m [,1] [,2] [1,] 1 4 [2,] 3 6
> h <- matrix(c(1,2,3,4,5,6,7,8,9),nrow=3) > h [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 > mean(h[h[,2]>4,1]) [1] 2.5 > mean(h[h[,3]<=8,2]) [1] 4.5 > nrow(h[h[,3]>7,]) [1] 2 > nrow(h[h[,3]>8,]) NULL > x <-matrix(c(1,2,3,2,3,4),nrow=3) > x [,1] [,2] [1,] 1 2 [2,] 2 3 [3,] 3 4 > x[x[,2] >= 3,] [,1] [,2] [1,] 2 3 [2,] 3 4 > j <- x[,2] >= 3 > j [1] FALSE TRUE TRUE > x[j,] [,1] [,2] [1,] 2 3 [2,] 3 4 > m <- matrix(c(1,2,3,4,5,6),nrow=3) > m [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > m[m[,1] > 1,] [,1] [,2] [1,] 2 5 [2,] 3 6 > m[m[,1] > 1 & m[,2] > 5,] [1] 3 6
apply() :
z <- matrix(c(1,2,3,4,5,6), nrow=3) > apply(z,2,mean) [1] 2 5 > f <- function(x) x/c(2,8) > y <- apply(z,1,f) > y [,1] [,2] [,3] [1,] 0.5 1.000 1.50 [2,] 0.5 0.625 0.75
sapply() :
> z12 <- function(z) return(c(z,z^2)) > sapply(1:8,z12) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 1 2 3 4 5 6 7 8 [2,] 1 4 9 16 25 36 49 64
> z <- matrix(1:8,nrow=4) > z [,1] [,2] [1,] 1 5 [2,] 2 6 [3,] 3 7 [4,] 4 8 > length(z) [1] 8 > class(z) [1] "matrix" > attributes(z) $dim [1] 4 2 > dim(z) [1] 4 2 > nrow(z) [1] 4 > ncol(z) [1] 2
> m <- matrix(c(1,2,3,4,5,6),nrow=3) > m [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > t(m) [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6
> j <- list(name="Joe", salary=55000, union=T) > j $name [1] "Joe" $salary [1] 55000 $union [1] TRUE
> names(j) [1] "name" "salary" "union"
unlist() :
> ulj <- unlist(j) > ulj name salary union "Joe" "55000" "TRUE" > class(ulj) [1] "character" > x <- list(abc=2, de=5) > name(x) Error: could not find function "name" > names(x) [1] "abc" "de" > ulx <- unlist(x) > ulx abc de 2 5 > class(ulx) [1] "numeric"
> j $name [1] "Joe" $salary [1] 55000 $union [1] TRUE > j[[1]] [1] "Joe" > j[2:3] $salary [1] 55000 $union [1] TRUE
> z <- list(a="abc",b=12) > z $a [1] "abc" $b [1] 12 > z$c <- 1 add element 3 > z $a [1] "abc" $b [1] 12 $c [1] 1 > z[[1]] <- NULL # delete element 1 > z $b [1] 12 $c [1] 1
lapply() :
> list(1:2,25:29) [[1]] [1] 1 2 [[2]] [1] 25 26 27 28 29 > lb <-list(1:3,25:27) > lb [[1]] [1] 1 2 3 [[2]] [1] 25 26 27 > lapply(lb,median) [[1]] [1] 2 [[2]] [1] 26 > as.numeric(lapply(list(1:3,25:27),median)) [1] 2 26
> b <- list(u = 5, v = 12) > c <- list(w = 13) > a <- list(b,c) > a [[1]] [[1]]$u [1] 5 [[1]]$v [1] 12 [[2]] [[2]]$w [1] 13
> length(j) [1] 3
head() :
> head(women)
height weight
1 58 115
2 59 117
3 60 120
4 61 123
5 62 126
6 63 129
rowMeans() :
> rowMeans(women) [1] 86.5 88.0 90.0 92.0 94.0 96.0 98.0 100.0 102.5 104.5 107.0 109.5 [13] 112.0 115.0 118.0
colMeans() :
> colMeans(women) height weight 65.0000 136.7333
rbind() :
> rbind(women,women) height weight 1 58 115 2 59 117 3 60 120 4 61 123 5 62 126 6 63 129 7 64 132 8 65 135 9 66 139 10 67 142 11 68 146 12 69 150 13 70 154 14 71 159 15 72 164 16 58 115 17 59 117 18 60 120 19 61 123 20 62 126 21 63 129 22 64 132 23 65 135 24 66 139 25 67 142 26 68 146 27 69 150 28 70 154 29 71 159 30 72 164
cbind() :
> cbind(women,women) height weight height weight 1 58 115 58 115 2 59 117 59 117 3 60 120 60 120 4 61 123 61 123 5 62 126 62 126 6 63 129 63 129 7 64 132 64 132 8 65 135 65 135 9 66 139 66 139 10 67 142 67 142 11 68 146 68 146 12 69 150 69 150 13 70 154 70 154 14 71 159 71 159 15 72 164 72 164
> data <- data.frame (id=1:3, weight=c(20,27,24)) > data id weight 1 1 20 2 2 27 3 3 24 > z <- data.frame(cbind(c(1,2),c(3,4))) > z X1 X2 1 1 3 2 2 4 > x <- matrix(c(1,2,3,4),nrow=2,ncol=2) > x <- data.frame(x) > x X1 X2 1 1 3 2 2 4
Ways to add a column
data$size <- c("small", "large", "medium") data[["size"]] <- c("small", "large", "medium") data["size"] <- c("small", "large", "medium") data[,"size"] <- c("small", "large", "medium") data$size <- 0
Ways to remove the column
data$size <- NULL data[["size"]] <- NULL data["size"] <- NULL data[,"size"] <- NULL data[3] <- NULL data[,3] <- NULL data <- subset(data, select=-size)
> women[1,] height weight 1 58 115 > women[1,2] [1] 115 > women[c(1:3),2] [1] 115 117 120 > women[1:4,] height weight 1 58 115 2 59 117 3 60 120 4 61 123 > women[-1:-10,] height weight 11 68 146 12 69 150 13 70 154 14 71 159 15 72 164
> colnames(z) <- c("good", "better") > z good better 1 1 3 2 2 4
> apply(women,2,mean) height weight 65.0000 136.7333
> attributes(x) $names [1] "X1" "X2" $row.names [1] 1 2 $class [1] "data.frame" > dim(x) [1] 2 2
> y <- factor(c("a","b","a","a","b")) > y [1] a b a a b Levels: a b > class(f) Error: object 'f' not found > class(y) [1] "factor"
as.vector() :
> yv <- as.vector(y) > yv [1] "a" "b" "a" "a" "b" > class(yv) [1] "character"
> yf <- factor(y, levels=c("a")) > yf [1] a <NA> a a <NA> Levels: a
> levels(y) <- c("two","one") > y [1] two one two two one Levels: two one
data <- data.frame (VoteX=c("Yes","Yes","No","Not Sure","No"), VoteLastTime=c("Yes","No","No","Yes","No")) > data VoteX VoteLastTime 1 Yes Yes 2 Yes No 3 No No 4 Not Sure Yes 5 No No > ct <- table(data) > ct VoteLastTime VoteX No Yes No 2 0 Not Sure 0 1 Yes 1 1 > class(ct) [1] "table"
> ctdf <- as.data.frame(ct) > ctdf VoteX VoteLastTime Freq 1 No No 2 2 Not Sure No 0 3 Yes No 1 4 No Yes 0 5 Not Sure Yes 1 6 Yes Yes 1 > class(ctdf) [1] "data.frame"
> x <- c (5,12,13,3,4,5) > table(x==5) FALSE TRUE 4 2