List in R Programming

 DATA TYPE:



Graphical Representation of Data Types in R is shown as below:


LIST:

An ordered collection of objects. List is Single Dimensional.


CREATING A LIST:

Let us create list "L1" with  different vector data type

L1<-list(1,c(1,2,3),'a',2+3i,TRUE)
print(L1)
[[1]]
[1] 1

[[2]]
[1] 1 2 3

[[3]]
[1] "a"

[[4]]
[1] 2+3i

[[5]]
[1] TRUE

NAMING LIST: 

We can name the elements in the list. Let us create a list "Stud_data"

Stud_data<-list(c("Anu","Abi"),c(100,95),list("Maths"))
print(Stud_data)
[[1]]
[1] "Anu" "Abi"

[[2]]
[1] 100  95

[[3]]
[[3]][[1]]
[1] "Maths"

names(Stud_data)<- c("Student Name", "Marks Obtained", "Subject")
print(Stud_data)
$`Student Name`
[1] "Anu" "Abi"

$`Marks Obtained`
[1] 100  95

$Subject
$Subject[[1]]
[1] "Maths"

ACCESSING LIST:

We can access the list using the index of the element or using the name. Let us access the list "Stud_data" using both methods

#Accessing list using Index
print(Stud_data[1])
$`Student Name`
[1] "Anu" "Abi"

#Accessing 2nd value from the first element of the list
print(Stud_data[[1]][2])
[1] "Abi"

#Accessing list using Name
print(Stud_data$`Marks Obtained`)
[1] 100  95

ADDING NEW ELEMENT:

We can add a new element to the list. Let us add the Department of the student to "Stud_data" list.

Stud_data[4]<-"B.Sc"

names(Stud_data)<- c("Student Name", "Marks Obtained", "Subject","Department")

print(Stud_data)

$`Student Name`
[1] "Anu" "Abi"

$`Marks Obtained`
     [,1] [,2]
[1,]  100   95

$Subject
$Subject[[1]]
[1] "Maths"


$Department
[1] "B.Sc"

UPDATING EXISTING ELEMENT:

We can update the existing element of the list. Let us change the Department of the "Stud_data" from "B.sc" to "BCA"

Stud_data[4]<-"BCA"
print(Stud_data)

$`Student Name`
[1] "Anu" "Abi"

$`Marks Obtained`
     [,1] [,2]
[1,]  100   95

$Subject
$Subject[[1]]
[1] "Maths"


$Department
[1] "BCA"

REMOVING ELEMENT FROM THE LIST:

Let us remove the "Marks Obtained" from the list "Stud_data"

Stud_data[2]<-NULL
print(Stud_data)

$`Student Name`
[1] "Anu" "Abi"

$Subject
$Subject[[1]]
[1] "Maths"


$Department
[1] "BCA"

MERGING LIST:

We can merge two or more list into one list using c() function

L1<-list(1,c(1,2,3),'a',2+3i,TRUE)
L2<-list("Hi","Hello",1)
NewList<-c(L1,L2)
print(NewList)

[[1]]
[1] 1

[[2]]
[1] 1 2 3

[[3]]
[1] "a"

[[4]]
[1] 2+3i

[[5]]
[1] TRUE

[[6]]
[1] "Hi"

[[7]]
[1] "Hello"

[[8]]
[1] 1

CONVERTING LIST TO VECTOR:

We can convert a list to a vector using the unlist function.

class(L1)
[1] "list"

L1<-unlist(L1)
print(L1)
[1] "1"    "1"    "2"    "3"    "a"    "2+3i" "TRUE"

class(L1)
[1] "character"

Note: To check whether a given input is a list data type we can use is.list() function. It returns TRUE if the data type is LIST else FALSE

is.list(L1) #L1 is vector since we have converted list to vector using unlist function
[1] FALSE

is.list(L2)
[1] TRUE

Comments

  1. Thanks Niranjana.. Its a quick refresh...kinda book in hand

    ReplyDelete
  2. Thanks Niranjana.. Its a quick refresh...kinda book in hand

    ReplyDelete
    Replies
    1. Happy that this post helps 👍. To View More Post, you can access https://rcrackerjack.blogspot.com/

      Delete

Post a Comment

Popular posts from this blog

Vector Data Type in R Programming

Arrays in R Programming

Variables in R Programming