data types in R programming

 R is a programming language that supports several data types. Here are some common data types in R:


1. Numeric: Represents real numbers (e.g., 3.14, -2.5).


   R

   x <- 3.14

    y<-5

   


2. Integer: Represents integer numbers (e.g., 1, -5).


   R

   y <- 5L  # L indicates an integer

   


3. Character: Represents text or strings.


   R

   z <- "Hello, World!"

   


4. Logical: Represents Boolean values (TRUE or FALSE).


   R

   is_r_programming_fun <- TRUE

   


5. Factor: Represents categorical data with predefined levels.


   R

   gender <- c("Male", "Female", "Male", "Female")

   



To check the data type of a variable, you can use the class() function or the typeof() function:


R

# Using class()

class(x)  # Returns the class of x


# Using typeof()

typeof(y)  # Returns the type of y



For example, if you want to check the data type of a variable x, you can use class(x) or typeof(x). Both functions will give you information about the data type of the variable.


R

# Example

x <- 3.14

class(x)   # Returns "numeric"

typeof(x)  # Returns "double"



5 ^3


Operator Name Example

+ Addition x + y

- Subtraction x - y

* Multiplication x * y

/ Division x / y

^(xor) Exponent(power) x ^ y [shift+6]

%% Modulus (Remainder from division) x %% y

%/% Integer Division x%/%y



miscllaneous operators:

=======================

: Creates a series of numbers in a sequence x <- 1:10




if statement

============

used to execute a block of code if condition is true.


syntax:

if(condition)

{

//code if condition is  true

}


example:

ae<-6

if(ae%%2==0)

{

  print("even")

}




if else statement

====================


operators to check conditions:

>

<

>=

<=

==

!=


example:

--------

n<-9

if(n%%2==0)

{

  print("number is even")

} else {

  print("number is odd")

}


if else if

==========

syntax:

if(condition)

{

//code if condition is true

}

else if(condition2)

{

//code if condition2 is true

}

else

{

//if all conditions are false

}





example:

---------

a<-23

b<-4

task<-"*"

if(task=="+")

{

  print(a+b)

} else if(task=="-"){

  print(a-b)

} else {

  print("invalid")

}


function

===========

1.function is a block of code that executes when it is called.

2.function keyword is used to create a function.


syntax:

func_name <- function(arg_if_any)

{

 //block of code

 return()//if any

}


example:

tryfunction<-function(n=0){

  if(n<0)

  {

    print("negative")

  } else {

    "positive"

  }

}

tryfunction(-7)






recursion: function calling itself with a condition.

==========

example:

tryfunction<-function(n=0){

  if(n>0)

  {

    print(n)

    n<-n-1

    tryfunction(n)

  }

}

tryfunction(6)




data structures

==================


vector:

R Vectors are the same as the arrays in R language which are used to hold multiple data values of the same type. 


One major key point is that in R Programming Language the indexing of the vector will start from '1' and not from '0'. 


We can create numeric vectors and character vectors as well.


syntax:

var_name<-c()


example:

[-------]

data<-c(5,8,0,3,4)

print(data)

for(rtg in data)

{

  print(rtg)

}





matrix

========

matrix(g<-matrix(c(45,3,345,5.6),nrow=2,ncol=2))


list(c(4,56,64))











In R programming, several built-in data structures are used to organize and store data efficiently. Here are some commonly used data structures in R:


Vectors:


A vector is a basic data structure in R and is a one-dimensional array that can hold elements of the same data type.

You can create a vector using the c() function.


# Example of creating a vector

my_vector <- c(1, 2, 3, 4, 5)



SORT THE VECTOR

=================

sort(vector_var)


vector

-------

data<-c(4,6,33,5.4)

sort(data)

length(data)

data[c(1)]

data[c(-2)]



Matrices:

=========

A matrix is a two-dimensional array that contains elements of the same data type.

You can create a matrix using the matrix() function.


# Example of creating a matrix

my_matrix <- matrix(1:6, nrow = 2, ncol = 3)




Arrays:

=======

An array is a multi-dimensional extension of a matrix. It can have more than two dimensions.

You can create an array using the array() function.


# Example of creating a 3D array

my_array <- array(1:24, dim = c(r,c,t))


my_array <- array(1:24, dim = c(2, 3, 4))



Lists:

======

A list is a versatile data structure that can contain elements of different data types. 


It is often used to store heterogeneous data.


You can create a list using the list() function.


# Example of creating a list

my_list <- list(name = "John", age = 30, grades = c(90, 85, 92))


check existence of a data in list

------------------------------------

thislist <- list("apple", "banana", "cherry")

"apple" %in% thislist


add a element

-------------

append(thislist, "orange")



itr<-function(l)

{

  for(a in l)

  {

    print(a)

  }

  

}


t<-list(5,33,2,2,1)

itr(t)

t<-append(t,5.6)

itr(t)






Data Frames:

============

A data frame is a two-dimensional table similar to a matrix, but it can store columns of different data types.

You can create a data frame using the data.frame() function.


# Example of creating a data frame

my_dataframe <- data.frame(name = c("John", "Jane", "Bob"), age = c(25, 30, 22))



names<-c("lakshmi","rahul gandhi","satish tiwari","modi")

ages<-c(4,5,3,4)

d<-data.frame(name=names,age=ages)

print(d)




Factors:

========

A factor is used to represent categorical data in R. It is a vector that can take on a limited set of distinct values.

You can create a factor using the factor() function.


# Example of creating a factor

my_factor <- factor(c("Male", "Female", "Male", "Female"))







0 Comments