## R Markdown

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:

##Qustion 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")
## [[1]]
## [1] "Python"
## 
## [[2]]
## [1] "PHP"
## 
## [[3]]
## [1] "Java"
print("List:")
## [1] "List:"
print(list_data)
## [[1]]
## [1] "Red"   "Green" "Black"
ld_len<-list_data[[1]]
ld_len
## [1] "Red"   "Green" "Black"
print("Number of objects in the said list::")
## [1] "Number of objects in the said list::"
length(ld_len)
## [1] 3

##Question 2 : Write a R program to assign NULL to 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 2nd and 3rd elements to NULL")
## [1] "Set 2nd and 3rd elements to NULL"
l[c(2,3)]<-c(NULL,NULL)
print(l)
## [[1]]
## [1] 1
## 
## [[2]]
## [1] 4
## 
## [[3]]
## [1] 5

##Question 3: Write a 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"
firstvector<-list1[[1]]
newlist=firstvector+10
print("New list:")
## [1] "New list:"
print(newlist)
##  [1] 11 12 13 14 15 16 17 18 19 20

##Question 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”)

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"
first_vec=list1[[1]]
firstvector=first_vec[-3]
print("First vector:")
## [1] "First vector:"
print(firstvector)
## [1]  1  2  4  5  6  7  8  9 10

##Question 5: Write a 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 Programmin",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 Programmin"
## 
## $g3
## [1] "HTML"
newlist<-list(g4="Python")
list1<-c(list1,newlist)
print("Add a new vector to the said list:")
## [1] "Add a new vector to the said list:"
print(list1)
## $g1
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $g2
## [1] "R Programmin"
## 
## $g3
## [1] "HTML"
## 
## $g4
## [1] "Python"

##Question 6: Write a 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 Programmin",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 Programmin"
## 
## $g3
## [1] "HTML"
g1<-list1[[1]]
g2<-list1[[2]]
print("Length of the vector g1 and g2 of the said list")
## [1] "Length of the vector g1 and g2 of the said list"
length(g1)
## [1] 10
length(g2)
## [1] 1

##Question 7: Write 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")
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"
newlist<-setdiff(l2,l1)
print("All elements of l2 that are not in l1:")
## [1] "All elements of l2 that are not in l1:"
setdiff(l2,l1)
## [[1]]
## [1] "X"
## 
## [[2]]
## [1] "Y"
## 
## [[3]]
## [1] "Z"