내용

글번호 763
작성자 허진경
작성일 2017-10-14 19:21:17
제목 R day1 실습
내용 x <- 100 y <- 200 x + y R.version iris # 1. 도움말 ?iris help("iris") ??iris help.search("iris") library(help="stats") methods(as) args(data) args(lm) ?lm attributes(iris) example(mean) ?mean # 주석입니다. R.version # 2. 패키지 install.packages("arules") library(arules) iris a detach("package:datasets", unload=TRUE) data(iris) iris library(datasets) data(iris) iris # 3. 변수 # 숫자, 알파벳, _, . 로 구성 # 시작 문자는 알파벳 또는 .으로 시작 # .은 히든변수 # <-, ->, <<-, ->>, = result <- 0 add <- function(a, b) { return (a + b) } add(1,2) result <- add(1,2) result2 <- 0 add2 <- function(a, b) { result2 <<- a + b # 전역변수에 값 할당 return (result) } add2(1,2) result2 ls() ls.str() rm(x) ls() rm(y) ls() ?rm ls() rm(list=ls()) ls() # 4. 출력 x <- 3 y <- 5 x+y z <- x + y z rm(z) (z <- x + y) print(z) cat(1,2,3,4) paste("Hello", "World") paste("Hello", "World", sep=",") paste0("Hello", "World") month.name paste0(1:12, c("st", "nd", "rd", rep("th", 9))) nth <- paste0(1:12, c("st", "nd", "rd", rep("th", 9))) month.name # January: 1st; February: 2nd; paste(month.name, nth, sep=": ") paste(month.name, nth, sep=": ", collapse="; ") # 2장. R 데이터 종류 및 구조 a <- "Hello" is.character(a) a <- 10 is.character(a) a <- "Hello" b <- 10 is.numeric(b) c <- TRUE is.logical(c) class(a) class(b) class(c) str(a) str(iris) d <- c(2,4,NA,6) #vector d is.na(d) is.na(d[3]) complete.cases(d) mean(d) mean(d, na.rm=TRUE) mean(na.omit(d)) # factor, 범주형 변수 gender <- c("남", "여", "남", "남", "여") gender as.factor(gender) gender <- factor(c("남", "여", "남", "남", "여"), levels=c("남", "여")) gender nlevels(gender) levels(gender) class(gender) str(gender) gender <- factor(c("남", "여", "남", "남", "여"), levels=c("남", "여"), ordered=TRUE) gender # vector, c() data <- c(1,2,3) data NROW(data) names(data) names(data) <- c("열1", "열2", "열3") names(data) data["열1"] data[1] data[-1] data[c(1,3)] data[data>2] data[c(FALSE, FALSE, TRUE)] data[c(TRUE, FALSE, TRUE)] a <- c(1,2,3) class(a) a <- numeric(3) a[1] <- 1 a[2] <- 2 a[3] <- 3 class(a) a b <- c("Hello", "World") b c <- c(TRUE, FALSE, TRUE, FALSE) (z <- c(a,b,c)) (z <- c(a,c)) a c(a, c(4,5,6)) append(a, c(4,5,6)) a <- 1:6 #c(1,2,3,4,5,6) b <- c(2,4,6,8,10,12) union(a,b) intersect(a,b) setdiff(a,b) a b a + b seq(1, 10) seq(0, 1) seq(1, 10, by=2) seq(0, 1, length.out=11) seq(10) 1:3 1:10 rep(1:4, 2) rep(1:4, each=2) rep(1:4, c(4,3,1,3)) rep(1:4, each=2, length=7) rep(1:4, each=2, times=3) ?rep