100% found this document useful (1 vote)
36 views5 pages

List Operations in R Programming

Uploaded by

kalpanaapj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
36 views5 pages

List Operations in R Programming

Uploaded by

kalpanaapj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

/*Program to perform Operations on Lists.

*/

SOURCE CODE:

print("Creating List :")

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

thislist

print("Accessing List elements")

thislist[1]

print("Changing item value")

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

thislist[1]<-"blackcurrant"

thislist

print("List length ")

length(thislist)

print("Check if item exists")

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

"apple" %in% thislist

print("Add list items")

append(thislist,"oranges")

print("Remove list items")

newlist<-thislist[-1]

print("Range of indexes")

firstlist<-list("apple","banana","cherry","oranges","mango")

(firstlist)[2:5]

print("Loop through the list")

for (x in firstlist)
{

print(x)

print("Adding two lists :")

list1 <- list("a", "b", "c")

list2 <- list(1,2,3)

list3 <- c(list1,list2)

list3

OUTPUT:

> print("Creating List :")

[1] "Creating List :"

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

> thislist

[[1]]

[1] "apple"

[[2]]

[1] "banana"

[[3]]

[1] "cherry"

> print("Accessing List elements")

[1] "Accessing List elements"

> thislist[1]

[[1]]

[1] "apple"
> print("Changing item value")

[1] "Changing item value"

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

> thislist[1]<-"blackcurrant"

> thislist

[[1]]

[1] "blackcurrant"

[[2]]

[1] "banana"

[[3]]

[1] "cherry"

> print("List length ")

[1] "List length "

> length(thislist)

[1] 3

> print("Check if item exists")

[1] "Check if item exists"

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

> "apple" %in% thislist

[1] TRUE

> print("Add list items")

[1] "Add list items"

> append(thislist,"oranges")

[[1]]
[1] "apple"

[[2]]

[1] "banana"

[[3]]

[1] "cherry"

[[4]]

[1] "oranges"

> print("Remove list items")

[1] "Remove list items"

> newlist<-thislist[-1]

> print("Range of indexes")

[1] "Range of indexes"

> firstlist<-list("apple","banana","cherry","oranges","mango")

> (firstlist)[2:5]

[[1]]

[1] "banana"

[[2]]

[1] "cherry"

[[3]]

[1] "oranges"

[[4]]

[1] "mango"

> print("Loop through the list")

[1] "Loop through the list"

[1] "apple"
[1] "banana"

[1] "cherry"

[1] "oranges"

[1] "mango"

> print("Adding two lists :")

[1] "Adding two lists :"

> list1 <- list("a", "b", "c")

> list2 <- list(1,2,3)

> list3 <- c(list1,list2)

> list3

[[1]]

[1] "a"

[[2]]

[1] "b"

[[3]]

[1] "c"

[[4]]

[1] 1

[[5]]

[1] 2

[[6]]

[1] 3

You might also like