0% found this document useful (0 votes)
22 views6 pages

R Console: Conditional & Loop Examples

The document consists of R code examples demonstrating conditional statements, implicit and explicit loops, and various statistical calculations. It includes operations such as determining the nature of roots of a quadratic equation, handling outliers in data, and calculating means and frequencies in datasets. The code also illustrates convergence of series through iterative methods.

Uploaded by

ankitasarkarsxc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views6 pages

R Console: Conditional & Loop Examples

The document consists of R code examples demonstrating conditional statements, implicit and explicit loops, and various statistical calculations. It includes operations such as determining the nature of roots of a quadratic equation, handling outliers in data, and calculating means and frequencies in datasets. The code also illustrates convergence of series through iterative methods.

Uploaded by

ankitasarkarsxc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

R Console Page 1

> #Conditional Statements


> #Q1.
> rm(list=ls())
> y=rnorm(3)
> a=y[1]
> b=y[2]
> c=y[3]
> d=(b**2)-(4*a*c)
> print(d)
[1] 0.1425484
> if (d>0)
+ {
+ print("The quadratic equation ax^2+bx+c=0 has two Distinct Real roots")
+ }else if(d<0)
+ {
+ print("The quadratic equation ax^2+bx+c=0 has two Imaginary Roots")
+ }else
+ {
+ print("The quadratic equation ax^2+bx+c=0 has two Equal Real roots")
+ }
[1] "The quadratic equation ax^2+bx+c=0 has two Distinct Real roots"
>
> #Q2.
> rm(list=ls())
> library("moments")
> x=rnorm(500)
> m=mean(x)
> s=sd(x)
> obs=x[x>m+3*s|x<m-3*s]
> n=length(obs)
> if(n>0)
+ {
+ print(paste("Since there are observations greater than mean+3sd or less than mean-3sd, the med
ian is",median(x)))
+ }else
+ {
+ print(paste("Since there are no observations greater than mean+3sd or less than mean-3sd, the
mean is",mean(x)))
+ }
[1] "Since there are observations greater than mean+3sd or less than mean-3sd, the median is -0.
0740077536316491"
>
> #Q3.
> rm(list=ls())
> library("moments")
> x=rnorm(500)
> m=mean(x)
> s=sd(x)
> a=x[x>m+3*s|x<m-3*s]
> n1=length(a)
> b=x[x>m+2*s|x<m-2*s]
> n2=length(b)-n1
> if(n2>0)
+ {
+ q1=quantile(x,0.25)
+ q3=quantile(x,0.75)
+ sk=(q1+q3-2*median(x))/(q3-q1)
+ print(paste("Since there are observations inside mean+3sd and mean-3sd but outside mean+2sd and
mean-2sd, the median will be",median(x),"and Bowley's Measure of Skewness will be",sk))
+ }else if(n1>0)
+ {
+ print(paste("Since there are observations greater than mean+3sd or less than mean-3sd, the med
ian is",median(x)))
+ }else
+ {
+
+ print(paste("Since all the observations lie within mean+2sd and mean-2sd, the mean is given by"
,mean(x)))
+ }
[1] "Since there are observations inside mean+3sd and mean-3sd but outside mean+2sd and mean-2sd,
R Console Page 2

the median will be -0.0629117392668715 and Bowley's Measure of Skewness will be 0.00509674816086
596"
>
>
> #Implicit Loops
> #Q1.
> math=c(87,65,32,99,47,65,76,81,55,69,85,90,49,76,21)
> stats=c(92,87,55,84,90,95,47,79,83,84,65,60,81,99,50)
> data=[Link]("Mathematics"=math,"Statistics"=stats)
> data
Mathematics Statistics
1 87 92
2 65 87
3 32 55
4 99 84
5 47 90
6 65 95
7 76 47
8 81 79
9 55 83
10 69 84
11 85 65
12 90 60
13 49 81
14 76 99
15 21 50
> sapply(data,mean)
Mathematics Statistics
66.46667 76.73333
> n=length(math)
> ((n-1)/n)*sapply(data,var)
Mathematics Statistics
456.7822 266.7289
>
> #Q2.
> cars=matrix(c(77,70,63,84,96,90,73,71,91,93,46,54,60,70,74,69,76,79,81,83),nrow=5,dimnames=list
(c(1,2,3,4,5),c("Type A","Type B","Type C","Type D")))
> cars
Type A Type B Type C Type D
1 77 90 46 69
2 70 73 54 76
3 63 71 60 79
4 84 91 70 81
5 96 93 74 83
> apply(cars,2,mean)
Type A Type B Type C Type D
78.0 83.6 60.8 77.6
>
> #Q3.
> A=c(2,5,9,1,7)
> B=c(3,3,1)
> C=c(5,7,3,8)
> rats=list(A,B,C)
> lapply(rats,mean)
[[1]]
[1] 4.8

[[2]]
[1] 2.333333

[[3]]
[1] 5.75

>
>
> #Explicit Loops
> #Q1.
> data=c(11,36,25,22,19,38,67,45,56,54,43,22,19,38,32,28,15,65,43,23,32,38,45,60,19,19,34,23,45,5
4,56,67,66,30,23,55)
> lcl=seq(10,60,10)
R Console Page 3

> ucl=seq(19,69,10)
> lcb=lcl-0.5
> ucb=ucl+0.5
> class_mark=(lcl+ucl)/2
> f=c(0,0,0,0,0,0)
> for (i in 1:6)
+ {
+ f[i]=length(data[data>lcb[i] & data<ucb[i]])
+ }
> cum_less=cumsum(f)
> cum_more=36-cum_less+f
> table=[Link]("class_limits"=paste(lcl,"-",ucl),"class_boundaries"=paste(lcb,"-",ucb),class_
mark,"freq"=f,"CF_less"=cum_less,"CF_more"=cum_more)
> table
class_limits class_boundaries class_mark freq CF_less CF_more
1 10 - 19 9.5 - 19.5 14.5 6 6 36
2 20 - 29 19.5 - 29.5 24.5 7 13 30
3 30 - 39 29.5 - 39.5 34.5 8 21 23
4 40 - 49 39.5 - 49.5 44.5 5 26 15
5 50 - 59 49.5 - 59.5 54.5 5 31 10
6 60 - 69 59.5 - 69.5 64.5 5 36 5
>
> #Q2.(a)
> rm(list=ls())
> x=0.5
> s1=1
> s2=1+x
> i=1
> while(s2-s1>0.000001)
+ {
+ i=i+1
+ s1=s2
+ s2=s2+(x**i)
+ print(paste(s1,",",s2))
+ }
[1] "1.5 , 1.75"
[1] "1.75 , 1.875"
[1] "1.875 , 1.9375"
[1] "1.9375 , 1.96875"
[1] "1.96875 , 1.984375"
[1] "1.984375 , 1.9921875"
[1] "1.9921875 , 1.99609375"
[1] "1.99609375 , 1.998046875"
[1] "1.998046875 , 1.9990234375"
[1] "1.9990234375 , 1.99951171875"
[1] "1.99951171875 , 1.999755859375"
[1] "1.999755859375 , 1.9998779296875"
[1] "1.9998779296875 , 1.99993896484375"
[1] "1.99993896484375 , 1.99996948242188"
[1] "1.99996948242188 , 1.99998474121094"
[1] "1.99998474121094 , 1.99999237060547"
[1] "1.99999237060547 , 1.99999618530273"
[1] "1.99999618530273 , 1.99999809265137"
[1] "1.99999809265137 , 1.99999904632568"
> print(s2)
[1] 1.999999
>
> #(b)
> rm(list=ls())
> x=0.5
> s1=1
> s2=1+(2*x)
> i=1
> repeat
+ {
+ s1=s2
+ s2=s1+((i+2)*x^(i+1))
+ print(paste(s1,",",s2))
+ i=i+1
+ if(abs(s2-s1)<0.000001)
R Console Page 4

+ {
+ break
+ }
+ }
[1] "2 , 2.75"
[1] "2.75 , 3.25"
[1] "3.25 , 3.5625"
[1] "3.5625 , 3.75"
[1] "3.75 , 3.859375"
[1] "3.859375 , 3.921875"
[1] "3.921875 , 3.95703125"
[1] "3.95703125 , 3.9765625"
[1] "3.9765625 , 3.9873046875"
[1] "3.9873046875 , 3.9931640625"
[1] "3.9931640625 , 3.996337890625"
[1] "3.996337890625 , 3.998046875"
[1] "3.998046875 , 3.99896240234375"
[1] "3.99896240234375 , 3.99945068359375"
[1] "3.99945068359375 , 3.99971008300781"
[1] "3.99971008300781 , 3.99984741210938"
[1] "3.99984741210938 , 3.99991989135742"
[1] "3.99991989135742 , 3.99995803833008"
[1] "3.99995803833008 , 3.99997806549072"
[1] "3.99997806549072 , 3.9999885559082"
[1] "3.9999885559082 , 3.99999403953552"
[1] "3.99999403953552 , 3.99999690055847"
[1] "3.99999690055847 , 3.99999839067459"
[1] "3.99999839067459 , 3.99999916553497"
> print(s2)
[1] 3.999999
>
> #(c)
> rm(list=ls())
> x=0.5
> s1=1
> s2=1-x
> i=1
> while(abs(s2-s1)>0.000001)
+ {
+ i=i+1
+ s1=s2
+ if(i%%2==0)
+ {
+ s2=s2+(x**i)
+ }else
+ {
+ s2=s2-(x**i)
+ }
+ print(paste(s1,",",s2))
+ }
[1] "0.5 , 0.75"
[1] "0.75 , 0.625"
[1] "0.625 , 0.6875"
[1] "0.6875 , 0.65625"
[1] "0.65625 , 0.671875"
[1] "0.671875 , 0.6640625"
[1] "0.6640625 , 0.66796875"
[1] "0.66796875 , 0.666015625"
[1] "0.666015625 , 0.6669921875"
[1] "0.6669921875 , 0.66650390625"
[1] "0.66650390625 , 0.666748046875"
[1] "0.666748046875 , 0.6666259765625"
[1] "0.6666259765625 , 0.66668701171875"
[1] "0.66668701171875 , 0.666656494140625"
[1] "0.666656494140625 , 0.666671752929688"
[1] "0.666671752929688 , 0.666664123535156"
[1] "0.666664123535156 , 0.666667938232422"
[1] "0.666667938232422 , 0.666666030883789"
[1] "0.666666030883789 , 0.666666984558105"
> print(s2)
R Console Page 5

[1] 0.666667
>
> #(d)
> rm(list=ls())
> x=0.5
> s1=1
> s2=1+x
> i=1
> f=1
> while(abs(s2-s1)>0.000001)
+ {
+ i=i+1
+ f=f*i
+ s1=s2
+ s2=s2+((x**i)/f)
+ print(paste(s1,",",s2))
+ }
[1] "1.5 , 1.625"
[1] "1.625 , 1.64583333333333"
[1] "1.64583333333333 , 1.6484375"
[1] "1.6484375 , 1.64869791666667"
[1] "1.64869791666667 , 1.64871961805556"
[1] "1.64871961805556 , 1.64872116815476"
[1] "1.64872116815476 , 1.64872126503596"
> print(s2)
[1] 1.648721
>
> #(e)
> rm(list=ls())
> x=0.5
> s1=0
> s2=x
> i=1
> f=1
> while(abs(s2-s1)>0.000001)
+ {
+ i=i+1
+ f=f*i
+ s1=s2
+ if(i%%2==0)
+ {
+ s2=s2-((x**i)/f)
+ }else
+ {
+ s2=s2+((x**i)/f)
+ }
+ print(paste(s1,",",s2))
+ }
[1] "0.5 , 0.375"
[1] "0.375 , 0.395833333333333"
[1] "0.395833333333333 , 0.393229166666667"
[1] "0.393229166666667 , 0.393489583333333"
[1] "0.393489583333333 , 0.393467881944444"
[1] "0.393467881944444 , 0.393469432043651"
[1] "0.393469432043651 , 0.39346933516245"
> print(s2)
[1] 0.3934693
>
> #(f)
> rm(list=ls())
> x=0.5
> s1=0
> s2=-x
> i=1
> f=1
> while(abs(s2-s1)>0.000001)
+ {
+ i=i+1
+ f=f*i
+ s1=s2
R Console Page 6

+ s2=s2-((x**i)/f)
+ print(paste(s1,",",s2))
+ }
[1] "-0.5 , -0.625"
[1] "-0.625 , -0.645833333333333"
[1] "-0.645833333333333 , -0.6484375"
[1] "-0.6484375 , -0.648697916666667"
[1] "-0.648697916666667 , -0.648719618055556"
[1] "-0.648719618055556 , -0.648721168154762"
[1] "-0.648721168154762 , -0.648721265035962"
> print(s2)
[1] -0.6487213
>
>

You might also like