0% found this document useful (0 votes)
5 views19 pages

R Programming Basics and Functions

Uploaded by

sreelakshmikn33
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)
5 views19 pages

R Programming Basics and Functions

Uploaded by

sreelakshmikn33
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

Module 1 Basics of R

Initiating R

First Program

sum(1:5)

[1] 15

Help in R
? name

help( ) or [Link]()

Assigning Variables

X<<- 3

assign(“x”,3)
assign('f',3*8)

assign(“G”,6*9,globalenv())

>f

[1] 24

> assign('f',3)

>f

[1] 3

> L<-sum(1:10);L

[1] 55

> (M<-sum(1:10))

[1] 55

Basic Mathematical operations

> 7:12+12:17
[1] 19 21 23 25 27 29

> c(1,2,3,4,5)+c(6,7,8,9,10)

[1] 7 9 11 13 15

> c(5,6,1,9)-2

[1] 3 4 -1 7

> c(5,6,1,9)-c(4,2,0,7)

[1] 1 4 1 2

> -1:4*-2:3

[1] 2 0 0 2 6 12

> -1:4*3

[1] -3 0 3 6 9 12

> 2^3

[1] 8

> 2**3

[1] 8

> identical(2^3,2**3)

[1] TRUE
> 5:9/2

[1] 2.5 3.0 3.5 4.0 4.5

> 5:9%/%2

[1] 2 3 3 4 4

> 5:9%%2

[1] 1 0 1 0 1
> c(2,4-2,1+1)==0

[1] FALSE FALSE FALSE

> c(2,4-2,1+1)==2

[1] TRUE TRUE TRUE

> 1:5==5:1

[1] FALSE FALSE TRUE FALSE FALSE

> 1:5!=5:1

[1] TRUE TRUE FALSE TRUE TRUE

> exp(1:3)<20

[1] TRUE TRUE FALSE

> (1:10)^2>=50

[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE

> sqrt(2)^2==2

[1] FALSE

> [Link](sqrt(2)^2,2)

[1] TRUE

> sqrt(2)^2-2

[1] 4.440892e-16

> [Link](sqrt(2)^2,3)

[1] "Mean relative difference: 0.5"


> isTRUE([Link](sqrt(2)^2,3))

[1] FALSE

c("Week","week","weak","WEEK")=="week"

[1] FALSE TRUE FALSE FALSE

> c("A","B","C")<"B"

[1] TRUE FALSE FALSE

> c("a","b","c")<"B"

[1] TRUE TRUE FALSE


search()

[1] ".GlobalEnv" "package:stats" "package:graphics"

[4] "package:grDevices" "package:utils" "package:datasets"

[7] "package:methods" "Autoloads" "package:base"


> [Link]("library")

[1] "C:/PROGRA~1/R/R-43~1.1/library"

> .Library

[1] "C:/PROGRA~1/R/R-43~1.1/library"

> [Link]("~")

[1] "C:\\SPB_DATA"

> [Link]("HOME")

[1] "C:\\SPB_DATA"
> .libPaths()

[1] "C:/Users/Anitha D B/AppData/Local/R/win-library/4.3"

[2] "C:/Program Files/R/R-4.3.1/library"


newenvironment<-[Link]()

> newenvironment[["variable1"]]<-c(4,5,7)

> newenvironment$"variable2"<-TRUE

> assign("variable3", c(1:5),newenvironment)

> newenvironment$"variable2"

[1] TRUE

> newenvironment[["variable1"]]
[1] 4 5 7

> get("variable3",newenvironment)

[1] 1 2 3 4 5

> ls(envir=newenvironment)

[1] "variable1" "variable2" "variable3"

> [Link](envir=newenvironment)

variable1 : num [1:3] 4 5 7

variable2 : logi TRUE

variable3 : int [1:5] 1 2 3 4 5

> exists("variable3",newenvironment)

[1] TRUE
> newlist<-[Link](newenvironment)

> newlist

$variable3

[1] 1 2 3 4 5

$variable1

[1] 4 5 7

$variable2

[1] TRUE
> cube<-function(x){cu<-x^3}

> z<-cube(5)

>z

[1] 125

> formals(cube)

$x

> args(cube)

function (x)

NULL

> formalArgs(cube)

[1] "x"

> body(cube)

cu <- x^3

}
> deparse(cube)

[1] "function (x) " "{" " cu <- x^3" "}"

> f1<-[Link](x=1:4,y=5:8)

> f2<-[Link](x=9:12,y=13:16)

> [Link](rbind,list(f1,f2))

x y

1 1 5

2 2 6

3 3 7

4 4 8

5 9 13

6 10 14

7 11 15

8 12 16
> [Link](function(x,y)x*y,list(1:3,4:6))

[1] 4 10 18
> x<-function(a1)

a2<-1

y<-function(a1)

a2/a1

y(a1)
}

> x(5)

[1] -0.2

random<- function(x)

If(rnorm(1)>0)

r<-1}

else
{r<-x}

Replicate(20,random(5))

You might also like