Practice Q1. Write an R program to count number of objects in a given list?

list_data <-  list( c(" Red "," Green "," Black "),list(" Python ", " PHP ", " Java "))
print(" List:")
## [1] " List:"
print(list_data)
## [[1]]
## [1] " Red "   " Green " " Black "
## 
## [[2]]
## [[2]][[1]]
## [1] " Python "
## 
## [[2]][[2]]
## [1] " PHP "
## 
## [[2]][[3]]
## [1] " Java "
print("Number    of    objects    in    the    said    list:")
## [1] "Number    of    objects    in    the    said    list:"
lengths(list_data)[1] + lengths(list_data)[2]
## [1] 6
### Additional information can be extracted as bellow,
str(list_data) #Metadata about the list (General information)
## List of 2
##  $ : chr [1:3] " Red " " Green " " Black "
##  $ :List of 3
##   ..$ : chr " Python "
##   ..$ : chr " PHP "
##   ..$ : chr " Java "
length(list_data) # Count the number of elements in the list
## [1] 2
lengths(list_data) #Count the number of objects within the list elements
## [1] 3 3

Practice Q2. Write an R program to assign NULL to a given list element?

l   =    list(1 ,   2 ,   3 ,   4 ,   5)
print(" Original    list:")
## [1] " Original    list:"
print(l)
## [[1]]
## [1] 1
## 
## [[2]]
## [1] 2
## 
## [[3]]
## [1] 3
## 
## [[4]]
## [1] 4
## 
## [[5]]
## [1] 5
print(" Set   2 nd    and   3 rd    elements    to    NULL ")
## [1] " Set   2 nd    and   3 rd    elements    to    NULL "
l[c(2,3)] = list(NULL)
print(l)
## [[1]]
## [1] 1
## 
## [[2]]
## NULL
## 
## [[3]]
## NULL
## 
## [[4]]
## [1] 4
## 
## [[5]]
## [1] 5

Practice Q3. Write an R program to Add 10 to each element of the first vector in a given list? Sample list: (g1 = 1:10, g2 = ”R Programming”, g3 = ”HTML”).

list1    <-   list( g1   =   1:10 ,    g2   =   " R    Programming ",   g3 = " HTML ")
print("Original    list:")
## [1] "Original    list:"
print( list1 )
## $g1
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $g2
## [1] " R    Programming "
## 
## $g3
## [1] " HTML "
print(" New    list:")
## [1] " New    list:"
s = rep(10,times =10)
list1[[1]] <- list1[[1]]+s
print(list1)
## $g1
##  [1] 11 12 13 14 15 16 17 18 19 20
## 
## $g2
## [1] " R    Programming "
## 
## $g3
## [1] " HTML "

Practice Q4. Write an R program to extract all elements of a first vector except the third element of it from a given list. Sample list: (g1 = 1:10, g2 = ”R Programming”, g3 = ”HTML”).

list1 = list( g1  = 1:10 , g2  = " R   Programming ", g3 =   " HTML ")
print(" Original    list:")
## [1] " Original    list:"
print( list1 )
## $g1
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $g2
## [1] " R   Programming "
## 
## $g3
## [1] " HTML "
print(" First    vector:")
## [1] " First    vector:"
list1[[1]][-3]
## [1]  1  2  4  5  6  7  8  9 10

Practice Q5. Write an R program to add a new item g4 = ”Python” to a given list. Sample list: (g1 = 1:10, g2 = ”R Programming”, g3 = ”HTML”).

list1 = list( g1  = 1:10 , g2  = " R   Programming ", g3 =   " HTML ")
print(" Original    list:")
## [1] " Original    list:"
print( list1 )
## $g1
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $g2
## [1] " R   Programming "
## 
## $g3
## [1] " HTML "
print(" Add   a   new    vector    to    the    said    list:")
## [1] " Add   a   new    vector    to    the    said    list:"
list1$g4 = "Python"
print(list1)
## $g1
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $g2
## [1] " R   Programming "
## 
## $g3
## [1] " HTML "
## 
## $g4
## [1] "Python"

Practice Q6. Write an R program to get the length of the first two vectors of a given list. Sample list: (g1 = 1:10, g2 = ”R Programming”, g3 = ”HTML”).

list1 = list( g1  = 1:10 , g2  = " R   Programming ", g3 =   " HTML ")
print(" Original    list:")
## [1] " Original    list:"
print( list1 )
## $g1
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $g2
## [1] " R   Programming "
## 
## $g3
## [1] " HTML "
print(" Length    of    the    vector    g1    and    g2    of    the    said list")
## [1] " Length    of    the    vector    g1    and    g2    of    the    said list"
cat("Length of g1 is" , lengths(list1)[1] , "and length of g2 is" , lengths(list1)[2]) #Count the number of objects within the list elements
## Length of g1 is 10 and length of g2 is 1

Practice Q7. Write an R program to find all elements of a given list that are not in another given list? Hint, see setdiff(..).

l1 = list ("x","y","z")
l2 = list ("X","Y","Z","x","y","z")
print("Original lists:")
## [1] "Original lists:"
print(l1)
## [[1]]
## [1] "x"
## 
## [[2]]
## [1] "y"
## 
## [[3]]
## [1] "z"
print(l2)
## [[1]]
## [1] "X"
## 
## [[2]]
## [1] "Y"
## 
## [[3]]
## [1] "Z"
## 
## [[4]]
## [1] "x"
## 
## [[5]]
## [1] "y"
## 
## [[6]]
## [1] "z"
print("All elements of l2 that arenot in l1 :")
## [1] "All elements of l2 that arenot in l1 :"
setdiff(l2,l1)
## [[1]]
## [1] "X"
## 
## [[2]]
## [1] "Y"
## 
## [[3]]
## [1] "Z"