1 R 2007 8 19 1 Windows R 1.1 R The R project web http://www.r-project.org/ R web Download [CRAN] CRAN Mirrors Japan Download and Install R [Windows 95 and later ] [base] 2.5.1 R - 2.5.1 for Windows R [R-2.5.1-win32.exe] 1.2 R Windows R [ ] [OK] R web 1
1.3 R web 1. RjpWiki http://www.okada.jp.org/rwiki/ 2. R-Tips http://cse.naro.affrc.go.jp/takezawa/r-tips/r.html 3. R http://aoki2.si.gunma-u.ac.jp/r/ 4. R R R http://www1.doshisha.ac.jp/ mjin/r/index.html R URL1 web URL2 R 1 URL3 web 1 URL4 R PDF 2 R 2.1 R R Microsoft Excel R R > R 2
> 12 + 3-67 # [1] -52 > 3 * 90 / 45 # [1] 6 > sqrt(2) # sqrt() [1] 1.414214 # 2 sqrt() 5 R > 12 ^ 2 # [1] 144 > sin(1) #sin" " [1] 0.841471 > cos(5) #cos" " [1] 0.2836622 > tan(3) #tan" " [1] -0.1425465 > log10(12) # ( 10 ) [1] 1.079181 > exp(1) # [1] 2.718282 R 3
+, -, *, / 2.2 R > a <- 244 # a > a # [1] 244 > b <- 0.12345 # b > b [1] 0.12345 > c <- "Katasho" # c > c [1] "Katasho" 1 1 R 4
> a <- c(1, 2, 3, 4, 5) # > a [1] 1 2 3 4 5 > b <- c("a", "b", "c", "d", "e") # > b [1] "a" "b" "c" "d" "e" > c <- c(1, "b", 3, "d", 5) # > c [1] "1" "b" "3" "d" "5" 1 c() 1 c 1 244 a a [1, 2, 3, 4, 5] 5 244 a [1, 2, 3, 4, 5] a 5 1 1 5
> dat <- matrix(c( + 1, 2, 3, + 4, 5, 6, + 7, 8, 9), nrow=3, ncol=3, byrow=t) > dat [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 > dat2 <- matrix(c( + 1, 2, 3, + 4, 5, 6, + 7, 8, 9), ncol=3) > dat2 [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 matrix() 2 matrix() nrow=3 3 ncol=3 3 3 3 ncol byrow byrow=t dat2 1 6
> dat4 <- cbind(x1, x2, x3) # cbind() > dat4 x1 x2 x3 [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 > dat5 <- rbind(x1, x2, x3) # rbind() > dat5 [,1] [,2] [,3] x1 1 2 3 x2 4 5 6 x3 7 8 9 7
> my.data <- data.frame(x1 = x1, X2 = x2, Y = x3) # > my.data X1 X2 Y 1 1 4 7 2 2 5 8 3 3 6 9 > attach(my.data) #my.data > X1 [1] 1 2 3 > X2 [1] 4 5 6 > Y [1] 7 8 9 > detach(my.data) # > X1 "X1" attach() detach() detach() 2.3 R 8
> dat <- rnorm(100, mean=50, sd=10) > mean(dat) # [1] 49.10156 > median(dat) # [1] 50.10624 > var(dat) # [1] 104.4191 > sd(dat) # [1] 10.21857 > dat2 <- rnorm(100, mean=100, sd=20) > var(dat, dat2) # [1] -34.2106 > cor(dat, dat2) # [1] -0.1832966 3 3.1 t t t 2 2 2 R R t t.test() paired paired=false t t var.equal=false 9
> group1 <- c(56, 49, 59, 42, 58, 61, 53) # > group2 <- c(73, 86, 54, 60, 65, 70, 53) # > t.test(group1, group2, var.equal=false) # t Welch Two Sample t-test data: group1 and group2 t = -2.3395, df = 9.518, p-value = 0.04259 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -23.2278060-0.4864797 sample estimates: mean of x mean of y 54.00000 65.85714 > t.test(group1, group2, paired=true) # t Paired t-test data: group1 and group2 t = -2.2632, df = 6, p-value = 0.06427 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -24.6769481 0.9626624 sample estimates: mean of the differences -11.85714 3.2 1 1 summary() aov() 1 2 3 10
10 1 > #1 > class.one <- c(3.9, 3.8, 3.6, 4.4, 4.7, 3.5, 3.7, 4.6, 3.9, 3.6) > #2 > class.two <- c(5.8, 4.4, 4.5, 6.4, 4.9, 7.2, 4.8, 5.7, 5.6, 3.4) > #3 > class.thr <- c(6.0, 5.8, 6.1, 5.6, 6.2, 6.0, 6.1, 5.7, 6.4, 5.9) > group <- as.factor(rep(c(1, 2, 3), c(10, 10, 10))) # > # > my.data <- data.frame( + Y = c(class.one, class.two, class.thr), X = group) > result <- summary(aov(y ~ X, my.data)) # result > result # Df Sum Sq Mean Sq F value Pr(>F) X 2 20.7807 10.3903 21.55 2.548e-06 *** Residuals 27 13.0180 0.4821 --- Signif. codes: 0 *** 0.001 ** 0.01 * 0.05. 0.1 1 t R web web 11
4 R Excel > x <- rnorm(100) > y <- rnorm(100) > plot(x, y) # > plot(x, y, xlim=c(-3,3), ylim=c(-3,3)) #x y > plot(x, y, xlab=" ", ylab=" ") #x y > plot(x, y, col="red", pch=18) # (col) (pch) R plot() plot() plot() > x <- 1:6 > y <- c(1, 5, 6, 3, 9, 10) > plot(x, y, type="b") # > plot(x, y, type="b", lty=2) # plot() type type= l type= s type= n plot() barplot() 12
# 1 > x <- c(12, 34, 72, 56, 20) > names(x) <- c("a1", "A2", "A3", "A4", "A5") > barplot(x, col="blue") > abline(h=0) # 2 > y <- c(17, 34, 69, 32, 12) > mat <- rbind(x, y) > mat A1 A2 A3 A4 A5 x 12 34 72 56 20 y 17 34 69 32 12 > barplot(mat, beside=true, col=c("blue", "red")) > legend(2, 60, + paste(c(" "," ")), + fill=c("blue", "red")) # k 3 > g1 <- rnorm(10, 10, 1) > g2 <- rnorm(10, 20, 1) > g3 <- rnorm(10, 5, 1) > #x > plot(x, c(g1,g2,g3), xlab="group", ylab="score", xaxt="n") > axis(1, at=1:3, label=c("g1", "g2", "g3")) #x > M <- c(mean(g1), mean(g2), mean(g3)) # > x.jiku <- 1:3 # x > points(x.jiku, M, pch=3, col="red", type="b") # 13
5 5.1 (1) (2) (3) 3 mean() mean () ( ) { } 5.2 pow 1 pow(2) 2 2 = 4 # pow() pow <- function(x){ } res <- x ^ 2 res # > pow(2)#2 [1] 4 > pow(12) #12 [1] 144 1 14
# pow2 pow2 <- function(x, y){ res <- x ^ y res } # > pow2(2, 4) #2 4 [1] 16 > pow2(2, -4) #2-4 [1] 0.0625 R Windows R [ ][ ] R R sd() sd() } sd2 <- function(x){ var2 <- function(x){ } # x if(is.vector(x)) n <- length(x) # x if(is.matrix(x)) n <- nrow(x) var2 <- var(x) * (n-1) / n # sd2 <- sqrt(var2(x)) # var2 sd2 15
1 R R 5.3 R 1 1 2 3 Excel R plot() #1 one.func <- function(x){ } 2*x + 1 # > plot(one.func) 1 3 1 > thr.func <- function(x) 3*x^3-0.5*x^2 + sqrt(x) + (3/4) > plot(thr.func) plot() points() legend() lwd 16
> plot(thr.func, xlim=c(-2,2), ylim=c(-5,5)) #thr.func plot() > abline(h=0) # x > abline(v=0) # y > x.dat <- seq(-2, 2, 0.1) #one.func x > points(x.dat, 2*x.dat+1, type="l", col="pink") #points() > legend(-1.5, 4, + c("one.func", "thr.func"), lwd=2, + col=c("black", "pink")) # 17