R : Copyright 2003, The R Development Core Team Version 1.8.0 (2003-10-08) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. [Previously saved workspace restored] > 7^2 [1] 49 > square <- function (x) x*x > square function (x) x*x > square(7) [1] 49 > > source("c:/quad.txt") > quadratic function (a,b,c) { d <- b^2 - 4*a*c c ((-b+sqrt(d))/(2*a), (-b-sqrt(d))/(2*a)) } > quadratic(1,0,-2) [1] 1.414214 -1.414214 > quadratic(5,3,-2) [1] 0.4 -1.0 > quadratic(5,3,2) [1] NaN NaN Warning messages: 1: NaNs produced in: sqrt(d) 2: NaNs produced in: sqrt(d) > > source("c:/quad.txt") > quadratic function (a,b,c) { d <- b^2 - 4*a*c if (d<0) stop("Discrimanent is negative") c ((-b+sqrt(d))/(2*a), (-b-sqrt(d))/(2*a)) } > quadratic(5,3,2) Error in quadratic(5, 3, 2) : Discrimanent is negative > > source("c:/quad.txt") > quadratic function (a,b,c) { d <- b^2 - 4*a*c c ((-b+sqrt(d+0i))/(2*a), (-b-sqrt(d+0i))/(2*a)) } > quadratic(5,3,2) [1] -0.3+0.5567764i -0.3-0.5567764i > quadratic(1,0,-2) [1] 1.414214+0i -1.414214+0i > > a<-9 > > a<10 [1] TRUE > a<=10 [1] TRUE > a < 10 && a > 5 [1] TRUE > a < 10 && a > 9 [1] FALSE > a < 10 || a > 9 [1] TRUE > > a <- c(4,7,8) > > if (a<10) cat("fred\n") fred Warning message: the condition has length > 1 and only the first element will be used in: if (a < 10) cat("fred\n") > a < 10 [1] TRUE TRUE TRUE > if (all(a<10)) cat("fred\n") fred > if (any(a<10)) cat("fred\n") fred > if (a>5) cat("fred\n") Warning message: the condition has length > 1 and only the first element will be used in: if (a > 5) cat("fred\n") > > i <- 1 > while (i<=10) + { print(i^2) + i <- i+1 + } [1] 1 [1] 4 [1] 9 [1] 16 [1] 25 [1] 36 [1] 49 [1] 64 [1] 81 [1] 100 > > i <- 1 > while (i<=10) + { cat(i^2,"\n") + i <- i+1 + } 1 4 9 16 25 36 49 64 81 100 > > for (i in 1:10) cat (i^2,"\n") 1 4 9 16 25 36 49 64 81 100 > > i <- 1 > repeat + { cat(i,i^2,"\n") + if (i==10) break; + i <- i+1 + } 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100 > > (1:10)^2 [1] 1 4 9 16 25 36 49 64 81 100 > > n <- 10 > > for (i in 1:n) print(i^2) [1] 1 [1] 4 [1] 9 [1] 16 [1] 25 [1] 36 [1] 49 [1] 64 [1] 81 [1] 100 > > n <- 1 > for (i in 1:n) print(i^2) [1] 1 > > n <- 0 > > for (i in 1:n) print(i^2) [1] 1 [1] 0 > > 1:10 [1] 1 2 3 4 5 6 7 8 9 10 > 10:1 [1] 10 9 8 7 6 5 4 3 2 1 > 1:0 [1] 1 0 > > m <- matrix(1:6, 3, 2) > m [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > > apply(m,2,mean) [1] 2 5 > apply(m,1,mean) [1] 2.5 3.5 4.5