#1.Write a 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:"
print(length(list_data))
## [1] 2
#2.Write a 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 "
#This is my answer
l[2] <- NULL
l[3] <- NULL
print(l)
## [[1]]
## [1] 1
##
## [[2]]
## [1] 3
##
## [[3]]
## [1] 5
# 3. Write a R program to Add 10 to each element of the first vector in a given list?
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:"
list1[[1]] <- list1[[1]] + 10
print(list1)
## $g1
## [1] 11 12 13 14 15 16 17 18 19 20
##
## $g2
## [1] "R Programming"
##
## $g3
## [1] "HTML"
#4.Write a 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").
g1<-list(1:10)
g2<-" R Programming "
g3<-" HTML "
list2 = list(g1, g2, g3)
print(list2)
## [[1]]
## [[1]][[1]]
## [1] 1 2 3 4 5 6 7 8 9 10
##
##
## [[2]]
## [1] " R Programming "
##
## [[3]]
## [1] " HTML "
ls<-list2[1][1]
print(ls)
## [[1]]
## [[1]][[1]]
## [1] 1 2 3 4 5 6 7 8 9 10
#5Write a R program to add a new item g4 = 鈥漃ython鈥? to a given list.
#Sample list: (g1 = 1:10, g2 = 鈥漅 Programming鈥?, g3 = 鈥滺TML鈥?).
g1<-list(1:10)
g2<-" R Programming "
g3<-" HTML "
g4<-"Python"
list3 =list(g1,g2,g3,g4)
print(list3)
## [[1]]
## [[1]][[1]]
## [1] 1 2 3 4 5 6 7 8 9 10
##
##
## [[2]]
## [1] " R Programming "
##
## [[3]]
## [1] " HTML "
##
## [[4]]
## [1] "Python"
#6Write a R program to get the length of the first
#two vectors of a given list.
#Sample list: (g1 = 1:10, g2 = 鈥漅 Programming鈥?, g3 = 鈥滺TML鈥?).
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:"
print(length(c(list1[[1]],list1[[2]])))
## [1] 11
#7 a 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")
print(l1)
## [[1]]
## [1] " x"
##
## [[2]]
## [1] " y"
##
## [[3]]
## [1] " z"
l2 <-list(" X", " Y", " Z", " x", " y", " 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 are not in l1:")
## [1] "All elements of l2 that are not in l1:"
print(setdiff(l2,l1))
## [[1]]
## [1] " X"
##
## [[2]]
## [1] " Y"
##
## [[3]]
## [1] " Z"
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.