R Data Types

R has following Data Types:

Character: To store string data. Character data can be declared with single or double quotes.
Examples: “k2”, “Hello World”, “23.4”,’Welcome’

> x="k2schools" > y='hello world' > class(x) [1] "character" > class(y) [1] "character"

Two characters can be concatenated using paste function.

> k1<-"Welcome to " > k2<-"k2schools.com" > paste(k1,k2) [1] "Welcome to k2schools.com"

Numeric (real or decimal): All decimal values are numerics in R Language. Even,if we assign integer value to variable, it will numeric in R.

Examples:

> p<-22 > q<-22.5 > r<-22.0 > class(p) [1] "numeric" > class(q) [1] "numeric" > class(r) [1] "numeric" > is.integer(q) #To check q is integer or not [1] FALSE

Integer: To create integer, we have to use as.integer function or append the L to each element when declaring.

Examples:

> m<-as.integer(12) > n<-13L > o<-as.integer("14") > m [1] 12 > n [1] 13 > o [1] 14 > class(m) #Test integer or not using class function in R [1] "integer" > class(n) #Test integer or not using class function in R [1] "integer" > class(o) #Test integer or not using class function in R [1] "integer" > is.integer(m) #Test integer or not using is.integer function in R [1] TRUE > is.integer(n) #Test integer or not using is.integer function in R [1] TRUE > is.integer(o) #Test integer or not using is.integer function in R [1] TRUE

Logical: Logical vectors store Boolean values TRUE or FALSE’s. Logical values are useful to compare.

Examples:

> s=5 > t=10 > u=s>t > u [1] FALSE > class(u) [1] "logical" > as.logical(u) [1] FALSE

Complex: Complex vectors can be created with complex. The vector can be specified either by giving its length,
its real and imaginary parts, or modulus and argument.

Syntax:

complex(length.out = 0, real = numeric(), imaginary = numeric(), modulus = 1, argument = 0)

Examples:

> c1<-4+5i > c1 [1] 4+5i > class(c1) [1] "complex" > is.complex(c1) [1] TRUE > c2<-complex(1,2,3) > c2 [1] 2+3i > is.complex(c2) [1] TRUE

Complex data type methods:

> c1 [1] 4+5i > c2 [1] 2+3i > c1+c2 [1] 6+8i > c1*c2 [1] -7+22i > c1-c2 [1] 2+2i > Re(c1) [1] 4 > Im(c1) [1] 5 > Mod(c1) [1] 6.403124 > Conj(c1) [1] 4-5i

Raw
Raw vector stores raw bytes of data.

Powered by WordPress