Sei sulla pagina 1di 7

R FUNDAMENTALS

version – To know about version of R


getwd() – To know about working directory
setwd("G:/R") – Set the WD to R folder in G drive.
ls() – Tells us all the custom variables we are using.

j=3
>j
[1] 3

ls()
[1] "j"

> m=5
> j*2+m
[1] 11
>j
[1] 3
> k=j*2+m
>k
[1] 11

source("text.txt")
[1] 100

DATA TYPES
 Character
 Numeric
 Integer
 Complex
 Boolean
> is.numeric(25.5)
[1] TRUE

> class(25.5)
[1] "numeric"

> class(7L)
[1] "integer"

> is.numeric(4L)
[1] TRUE

> class(5+7i)
[1] "complex"

15%%4
[1] 3

> 'madhu'
[1] "madhu"
> class(madhu)
Error: object 'madhu' not found
> class('madhu')
[1] "character"

paste('James','Bond')
[1] "James Bond"
> paste0('James','Bond')
[1] "JamesBond"

> substr("111, India",start=1,stop=4)


[1] "111,"

> sub("India","Romania","I live in India")


[1] "I live in Romania"

> sprintf("I live in %s","India")


[1] "I live in India"

> name="Madhu"
> name
[1] "Madhu"
> sprintf("Hi, Welcome %s", name)
[1] "Hi, Welcome Madhu"

> 4>5
[1] FALSE

> j=4>3
>j
[1] TRUE
> class(j)
[1] "logical"

> j==true
Error: object 'true' not found
> j==TRUE
[1] TRUE

> as.integer('7')+3
[1] 10

> as.integer('a')
[1] NA
Warning message:
NAs introduced by coercion

> as.integer(2+5i)
[1] 2
Warning message:
imaginary parts discarded in coercion

VECTORS
> my_vector<-c(1,10,20,14,13,18,225)
> my_vector
[1] 1 10 20 14 13 18 225

> class(my_vector)
[1] "numeric"
> typeof(my_vector)
[1] "double"

> char_vector<-c('madhu','dadi','hello')
> char_vector
[1] "madhu" "dadi" "hello"
> funny_vector<-c(10,'AAa',TRUE,'hey')
> funny_vector
[1] "10" "AAa" "TRUE" "hey"

> vector("integer",length=10)
[1] 0 0 0 0 0 0 0 0 0 0

> log_vector<-logical(10)
> log_vector
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

seq(1,19)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
> seq(53,56)
[1] 53 54 55 56
> seq(45,4)
[1] 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21
[26] 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4

> seq(3,45,4)
[1] 3 7 11 15 19 23 27 31 35 39 43

my_vector<-c(1,10,20,14,13,18,225)
> length(my_vector)
[1] 7

> vec6<-c(1,23,424,45,24,5)
> length(vec6)
[1] 6
> vec6[1]
[1] 1
> vec6[1:3]
[1] 1 12 13

> vec6
[1] 1 12 13 234 234324
> vec6[-3]
[1] 1 12 234 234324

> vec6[c(1,3,5)]
[1] 1 13 234324

> v2<-c(1,c(20,123,432),543,c(23,45))
> v2
[1] 1 20 123 432 543 23 45
> v2[3]
[1] 123

> new_weights<-c(a=12,b=232,c=23,d=56,e=76)
> new_weights
a b c d e
12 232 23 56 76
> new_weights[1]
a
12
> new_weights['a']
a
12
> attr(new_weights,"info")<-"Values represent weights in kg"
> new_weights
a b c d e
12 232 23 56 76
attr(,"info")
[1] "Values represent weights in kg"

MATRICES
> v1<-c(10,20,30,40)
> v1
[1] 10 20 30 40
> dim(v1)
NULL

> dim(v1)<-c(2,2)
> v1
[,1] [,2]
[1,] 10 30
[2,] 20 40
> dim(v1)
[1] 2 2
> is.matrix(v1)
[1] TRUE

> Matrix1<-matrix(c(10,9,8,5,45,3),nrow=2,ncol=3)
> View(Matrix1)

Potrebbero piacerti anche