Professional Documents
Culture Documents
R Programming Lisha
R Programming Lisha
> x<-1:4
> y<-6:9
> z<-x+y
>z
[1] 7 9 11 13
> x-y
[1] -5 -5 -5 -5
> x*y
[1] 6 14 24 36
> x/y
[1] 0.1666667 0.2857143 0.3750000 0.4444444
>#Logical Operation
> x>y
[1] FALSE FALSE FALSE FALSE
> x<y
[1] TRUE TRUE TRUE TRUE
> x>=y
[1] FALSE FALSE FALSE FALSE
> x==y
[1] FALSE FALSE FALSE FALSE
> x/y
[,1] [,2] [,3]
[1,] 0.1666667 0.4444444 0.1666667
[2,] 0.2857143 0.5000000 0.2857143
[3,] 0.3750000 0.5454545 0.3750000
CONTROL STRUCTURES
f<-function()
{
> num=as.integer(readline(prompt="Enter a number:"))
> sum=0
> temp = num
> while(temp > 0) {
+ digit = temp %% 10
+ sum = sum + (digit ^ 3)
+ temp = floor(temp / 10)
+}
> if(num == sum) {
+ print(paste(num, "is an Armstrong number"))
+ } else {
+ print(paste(num, "is not an Armstrong number"))
+}
}
Output:
Enter a number:23
[1] "23 is not an Armstrong number"
Enter a number:370
[1] "370 is an Armstrong number"
FUNCTIONS
f<-function()
{
> add<-function(x,y){
+ return(x+y)
+}
> subtract<-function(x,y){
+ return(x-y)
+}
> multiply<-function(x,y){
+ return(x*y)
+}
> divide<-function(x,y){
+ return(x/y)
+}
> print("SELECT OPERATION")
> print("1.ADD")
> print("2.SUBTRACT")
> print("3.MULTIPLY")
> print("4.DIVIDE")
> choice=as.integer(readline(prompt="Enter Choice[1/2/3/4]:"))
> num1=as.integer(readline(prompt="Enter first number:"))
> num2=as.integer(readline(prompt="Enter second number:"))
> result<-switch(choice,add(num1,num2),subtract(num1,num2),
multiply(num1,num2),divide(num1,num2))
> print(paste("Answer=",result))}
}
OUTPUT:
SCOPING RULES
> total=900
> open.account<-function()
+{
+ total<-200;
+ list(deposit=function(amount)
+{
+ if(amount<=0)
+ stop("Deposit must be positive!\n")
+ total<<-total+amount
+ cat(amount,"Deposited.Your balance is",total,"\n\n")
+ },
+ withdraw=function(amount){
+ if(amount>total)
+ stop("you don't have that much money!\n")
+ total<<-total-amount
+ cat(amount,"withdraw your balance is",total,"\n\n")
+ },
+ balance=function()
+{
+ cat("your balance is",total,"\n\n")
+}
+ )}
OUTPUT:
> total
[1] 900
> rio<-open.account()
> rio$deposit(300)
300 Deposited.Your balance is 500
> rio$withdraw(100)
100 withdraw your balance is 400
> rio$balance()
your balance is 400
LOOP FUNCTIONS
> A<-matrix(1:9,3,3)
> B<-matrix(4:15,4:4)
> C<-matrix(8:16,3,3)
> mylist<-list(A,B,C)
> lapply(A,mean)
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] 4
[[5]]
[1] 5
[[6]]
[1] 6
[[7]]
[1] 7
[[8]]
[1] 8
[[9]]
[1] 9
> lapply(mylist,"[",1,)
[[1]]
[1] 1 4 7
[[2]]
[1] 4 8 12
[[3]]
[1] 8 8
> sapply(mylist,"[",2,1)
[1] 2 5 9
> sapply(A,mean)
[1] 1 2 3 4 5 6 7 8 9
> mapply(rep,1:4,4:1)
[[1]]
[1] 1 1 1 1
[[2]]
[1] 2 2 2
[[3]]
[1] 3 3
[[4]]
[1] 4
> apply(A,2,mean)
[1] 2 5 8
> f<-gl(3,5)
> d<-c(1:15)
> tapply(d,f,mean)
1 2 3
3 8 13
> library(dplyr)
> s<-select(iris,c(2:5))
> head(s)
Sepal.Width Petal.Length Petal.Width Species
1 3.5 1.4 0.2 setosa
2 3.0 1.4 0.2 setosa
3 3.2 1.3 0.2 setosa
4 3.1 1.5 0.2 setosa
5 3.6 1.4 0.2 setosa
6 3.9 1.7 0.4 setosa
> f<-filter(iris,Species=="setosa")
> head(f,3)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
> c<-mutate(iris,Greaterhalf=Sepal.Width>0.5*Sepal.Length)
>c
> table(c$Greaterhalf)
FALSE TRUE
84 66
> a<-arrange(c,desc(Sepal.Width))
> head(a)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species Greaterhalf
1 5.7 4.4 1.5 0.4 setosa TRUE
2 5.5 4.2 1.4 0.2 setosa TRUE
3 5.2 4.1 1.5 0.1 setosa TRUE
4 5.8 4.0 1.2 0.2 setosa TRUE
5 5.4 3.9 1.7 0.4 setosa TRUE
6 5.4 3.9 1.3 0.4 setosa TRUE
> ss<-summarize(iris,nw=mean(Sepal.Width))
> head(ss)
nw
1 3.057333
> gp<-group_by(iris,Species)
> head(gp)
# A tibble: 6 x 5
# Groups: Species [1]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
<dbl> <dbl> <dbl> <dbl> <fct>
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
> nw<-summarize(gp,nw=mean(Sepal.Width))
> head(nw)
# A tibble: 3 x 2
Species nw
<fct> <dbl>
1 setosa 3.43
2 versicolor 2.77
3 virginica 2.97
PROFILING
> data<-as.data.frame(matrix(rnorm(4e5*150,mean=5),ncol=150))
> normcols<-function(d){
+ means<-apply(d,2,mean)
+ for(i in seq_along(means)){
+ d[, i]<-d[, i]- means[i]
+}
+d
+}
> Rprof()
> invisible(normcols(data))
> Rprof(NULL)
>
> summaryRprof()
$by.self
self.time self.pct total.time total.pct
"apply" 0.26 27.08 0.72 75.00
"normcols" 0.20 20.83 0.96 100.00
"unlist" 0.20 20.83 0.20 20.83
"aperm.default" 0.16 16.67 0.16 16.67
"mean.default" 0.08 8.33 0.08 8.33
"FUN" 0.02 2.08 0.10 10.42
"lazyLoadDBfetch" 0.02 2.08 0.02 2.08
"sys.call" 0.02 2.08 0.02 2.08
$by.total
total.time total.pct self.time self.pct
"normcols" 0.96 100.00 0.20 20.83
"apply" 0.72 75.00 0.26 27.08
"unlist" 0.20 20.83 0.20 20.83
"as.matrix" 0.20 20.83 0.00 0.00
"as.matrix.data.frame" 0.20 20.83 0.00 0.00
"aperm.default" 0.16 16.67 0.16 16.67
"aperm" 0.16 16.67 0.00 0.00
"FUN" 0.10 10.42 0.02 2.08
"mean.default" 0.08 8.33 0.08 8.33
"lazyLoadDBfetch" 0.02 2.08 0.02 2.08
"sys.call" 0.02 2.08 0.02 2.08
"%in%" 0.02 2.08 0.00 0.00
"[<-" 0.02 2.08 0.00 0.00
"[<-.data.frame" 0.02 2.08 0.00 0.00
"<Anonymous>" 0.02 2.08 0.00 0.00
"cmp" 0.02 2.08 0.00 0.00
"cmpCall" 0.02 2.08 0.00 0.00
"cmpfun" 0.02 2.08 0.00 0.00
"compiler:::tryCmpfun" 0.02 2.08 0.00 0.00
"doTryCatch" 0.02 2.08 0.00 0.00
"exists" 0.02 2.08 0.00 0.00
"genCode" 0.02 2.08 0.00 0.00
"getInlineHandler" 0.02 2.08 0.00 0.00
"tryCatch" 0.02 2.08 0.00 0.00
"tryCatchList" 0.02 2.08 0.00 0.00
"tryCatchOne" 0.02 2.08 0.00 0.00
"tryInline" 0.02 2.08 0.00 0.00
$sample.interval
[1] 0.02
$sampling.time
[1] 0.96
STATISTICAL SIMULATION
> coin=c("Head","Tail")
>c
function (...) .Primitive("c")
> set.seed(1)
> y=sample(coin,6,replace=TRUE)
>y
[1] "Head" "Tail" "Head" "Head" "Tail" "Head"
> length(y[y=="Head"])
[1] 4
> replicate(10,sample(coin,6,replace=TRUE))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "Head" "Head" "Tail" "Head" "Tail" "Tail" "Head" "Head" "Head" "Head"
[2,] "Head" "Head" "Head" "Head" "Tail" "Head" "Tail" "Head" "Head" "Tail"
[3,] "Tail" "Head" "Head" "Tail" "Head" "Tail" "Tail" "Tail" "Tail" "Tail"
[4,] "Tail" "Tail" "Head" "Head" "Tail" "Tail" "Tail" "Head" "Tail" "Tail"
[5,] "Head" "Tail" "Head" "Head" "Head" "Tail" "Tail" "Tail" "Tail" "Tail"
[6,] "Head" "Tail" "Head" "Tail" "Head" "Tail" "Tail" "Tail" "Head" "Tail"
> d=replicate(10,length(sample(coin,6,replace=TRUE)[y=="Head"]))
> mean(d)
[1] 3