Data structure is the form of organizing and storing the data.
R supports five basic types of data structures
vector
matrix
list
data frame
factor
A special type of vector that can contain elements of different classes(Integer ,charcter). List is a data structure having components of mixed data types.
A vector having all elements of the same type is called atomic vector but a vector having elements of different type is called list.
Use the list() function to create a list.
x = list(1, "a", TRUE) # we can use = or <- sign
x
## [[1]]
## [1] 1
##
## [[2]]
## [1] "a"
##
## [[3]]
## [1] TRUE
Another example, In the example bellow ,x, y and z are called tags which makes it easier to reference the components of the list.
However, tags are optional. We can create the same list without the tags as follows. In such scenario, numeric indices are used by default.
mylist=list("x"=5.78 , "y"=TRUE ,"z"=5:9)
mylist
## $x
## [1] 5.78
##
## $y
## [1] TRUE
##
## $z
## [1] 5 6 7 8 9
#typeoff function
typeof(mylist)
## [1] "list"
#length function
length(mylist)
## [1] 3
mylist[3]
## $z
## [1] 5 6 7 8 9
mylist$z
## [1] 5 6 7 8 9
####Merging List
Multiple lists can be merged with all those lists one single list() function.
list_a <- list(7,8,9, "AA")
list_b <- list(4,6,5,"bb")
list_combind <- c(list_a,list_b)
list_combind
## [[1]]
## [1] 7
##
## [[2]]
## [1] 8
##
## [[3]]
## [1] 9
##
## [[4]]
## [1] "AA"
##
## [[5]]
## [1] 4
##
## [[6]]
## [1] 6
##
## [[7]]
## [1] 5
##
## [[8]]
## [1] "bb"
We can also create an empty list of a prespecified length with the vector() function
x <- vector("list", length = 5)
x
## [[1]]
## NULL
##
## [[2]]
## NULL
##
## [[3]]
## NULL
##
## [[4]]
## NULL
##
## [[5]]
## NULL
(1)R Programming For Beginners Part 1
https://www.youtube.com/watch?v=DmX5TW473BA
(2)R tutorial by examples:Data Frame https://rpubs.com/sheikh/data_frame (3)Data Manipulation with dplyr using Covid19 Data https://rpubs.com/sheikh/dplyr (4)R Tutorial:Graphs using qplot https://rpubs.com/sheikh/qplot
(5)title: “R Data Structure: Vector”
https://rpubs.com/sheikh/vector