real <- 3.5 as.integer(real) intg <- 3 is.integer(intg) intg2 <- 1:5 is.integer(intg2) intg3 <- 1.5:3 is.integer(intg3) real <- 3 is.double(real) is.numeric(1:3) is.numeric(3.5) ch <- c("red","blue", "yellow") ch2 <- c('a','b',"c") test <- c(TRUE,FALSE,TRUE) is.logical(test) test2 <- c(T,F,T) as.numeric(test2) as.logical(c(0,1,2,-1)) x <- "char" mode(x) x <- 5 is.character(x) is.numeric(x) is.double(x) x <- "abc" as.double(x) x <- 1 as.character(x) as.character(123) vec <- c(1,3,4,2,5) # vector 정의는 c() 함수를 이용 vec vec[1] vec[3] num <- c(1,2,3) # numeric type ch <- c("red","blue","yellow") #character type lo <- c(T,F,F,T) #logical type num2 <- c(1,2,"red","blue") # character type lo2 <- c(T,F,T,1,3) #numeric lo3 <- c(T,F,"abc") #character type assign("x",1:10) x for(i in 1:5) assign(paste0("x",i),1:5) ls(); x2 x1 <- 1:10 (x=1:10) # local assign x1 x2 = 5:1 x3 <<- 1:10 # global assign x <- c(1,2,3) y <- c(1,2,3,4) v <- 2*x + y +1 # x=c(1,2,3,1) , 1 = (1,1,1,1) a = 3 b <- 2 c <- 3 + 3 a + b + c a - b * c a / b - c a ^ b * c a ^ (b + c) a %% b c %% b A <- T; B<- F C <- c(T,T); D<- c(F,T) A & B A && B C & D C && D A <- T; B <- F C <- c(T,T); D <- c(F,T) A | B A || B C | D C || D a <- c(1,2); b <- c(2,2) a < b a <= b a > b a >= b A <- c(T,T); B <- c(F,T); C <- c(F,F) A == B B == C all(A==B) any(A==B) any(A==C) A <- c(T,T); B <- c(F,T); C <- c(F,F) A != B B != C all(A!=B) any(A!=B) any(A!=C) 3 ^ 2 %% 4 3 * 2 %% 4 a = 1 a = a + 2 a d = 5 3 + 10 * ( 2 + 3 ) / 5 %% (d-1) 3 + 10 * ( 2 + 3 ) / (5 %% (d-1)) log(10) # 다른 밑 x를 갖는 경우: log(10, base=x) log10(10) log(exp(1)) exp(1) exp(2) sin(30) sin(pi/6) x <- c(1,3,2,5,10) max(x) min(x) x <- c(-5,10,2,5,8,9) range(x) sum(x) prod(x) mean(x) var(x) x <- c(1,5,3,2,4) ; sort(x) #sort(x, decreasing = T)로 하면 내림차순 order(x) # 2의 경우 원래 4번째에 위치 # x[order(x)]=1 2 3 4 5 y <- c("A","A","B","B","A") y[order(x)] # x의 정렬 반영 x <- 1:10 y <- 3.3:8 x; y; rep(c(1,2),times=3) rep(c(1,2),each=3) seq(from=10,to=1) seq(from=1,to=10) #seq(1,10) default by=1 seq(length=10,from=-5,by=3) seq(length=6 , to=10, by=2) vec <- vector() # 비어있는 객체 생성 vec <- c(3,6) # 숫자형 벡터 vec2 <- c("abc","def") # 문자형 벡터 vec3 <- c(T,F,F,T) # 논리형 벡터 names(vec) <- c("first", "second") vec4 <- vector() vec4[1] <- 1; vec4[2] <- 5; vec4 x1 <- matrix(1:10,nrow=5,ncol=2,byrow=T) x1 x2 <- matrix(1:10,5,2,byrow=F) # default = F x2 cbind(x1,x2) #행을 기준으로 열을 덧붙임. rbind(x1,x2) #열을 기준으로 행을 덧붙임. A <- matrix(1:12,4,3) rownames(A) <- c("n1","n2","n3","n4") colnames(A) <- paste0("x",1:3) A A['n1',] A[,"x3"] rname <- paste0("n",1:4) cname <- paste0("x",1:3) B <- matrix(1:12,4,3, dimnames=list(rname,cname)) B A <- matrix(1:12,4,3) A[1,2] A[2,3] A[1,] # 1행 선택 A[,2] # 2행 선택 A[c(1,3),] # 1,3 행의 선택 A[, 1:2] # 1,2 열의 선택 A[,-3] # 3열을 제외하고 선택 A <- matrix(1:12,4,3); B<- matrix(1,4,3) A + B; A - B A <- matrix(1:6,2,3); B <- matrix(1,3,2) A%*%B # 3x3 행렬 A <- matrix(1:4,2,2) solve(A) A <- matrix(1:12,4,3); B<- matrix(1,4,3) t(A); t(B) A <- matrix(1:9,3,3) sum(diag(A)) # diag(벡터): 대각행렬, diag(행렬):대각성분 x <- c(1,2,3); y<- c(1,2,3) sum(x*y) # 벡터에 대한 내적은 간단히 sum을 이용하여 계산 A <- matrix(1:4,2,2) det(A) A <- matrix(1:4,2,2) eigen(A) eigen(A)$values eigen(A)$vectors lst1 = list(a = 1:10, b= matrix(1:4,2,2)) lst2 <- list() lst2[[1]] <- matrix(1:10,5,2) lst2[[2]] <- lst1 lst1$a lst1$b lst1[1] #리스트 객체로 반환 lst1[[1]] #리스트의 원소를 나타낸다. (lst1$a와 동일) lst2[[1]] lst2[[1]][3] #인덱스의 이중 사용 가능 lst2[[2]][[1]] #인덱스의 이중 사용 가능 grade <- c("A","A","B","C","B","B") f.grade <- factor(grade) summary(f.grade) # order = T: 순서형, order=F: 명목형 grade <- c("A","A","B","C","B","B") f2.grade <- factor(grade, order=T) f2.grade #원하는 순서를 나타내기 위해 작은 값 순서로 levels에 정의 lev <- c("C","B","A") f3.grade <- factor(grade,levels=lev,order=T) f3.grade levels(f2.grade) levels(f3.grade) # 순서에 대한 정보는 없다. x1 = 1:4 x2 <- c("Kim","Lee","Jeong","Park") dat = data.frame(x1,x2) dat2 = data.frame(num=x1, name=x2) dat; dat2 dat2$no dat2$name #문자형 변수는 기본적으로 factor로 변환 # stringsAsFactors=F 를 이용 factor 자동변환 방지 dat3 = data.frame(x1,x2,stringsAsFactors=F) #행렬의 인덱스 형태의 사용 dat[,1]; dat[,2] dat[1,]; dat[2,] dat[2,1]; dat[3,2]; dat3[3,2] dat[1]; dat[2] dat[[1]]; dat[[2]] dat[[1]][2]; dat[[2]][1] dat[,1:2] dat[,-1] dat[-1,-2] dat[-1,c(2,4)] x1 <- array(1:24,dim=c(4,3,2)) x2 <- array(1:32,dim=c(2,2,4,2)) x1[,,1] # 4 x 3 행렬 x1[,,2] # 4 x 3 행렬 x2[,,3,1] # 2 x 2 행렬 x <- 1:100 ; sum1 <- sum(x[13:100]) x <- c(1:5,10:20,30:45,2:5,11:30) x>12 sum2 <- sum(x[x>12]) # sum all values between 10 and 20 sum3 <- sum(x[x>10 & x<20]) # Logical operator # & : and, | : or, == : equal, != : not equal # select elements at even positions in x x <- 1:100 x[seq(2,100,by=2)] x_mat <- matrix(x,20,5) # select column 1,3,5 in x_mat x[,c(1,3,5)] # x[,seq(1,5,by=2)] # select rows 2,4 in x_mat x[c(2,4),] # x[seq(2,4,by=2),] # For matrix, (i,j)th element of A = A[i,j] x_mat <- matrix(rnorm(100),20,5) # Calculate mean values of each columns apply(x_mat,2,mean) # Calculate mean values of each rows apply(x_mat,1,sum) # Calculate variance values of each columns apply(x_mat,2,var) # Calculate variance values of each rows apply(x_mat,1,var)