September 5, 2021

R Lists

Lists in R programming language

A List is a one dimensional R data structure, similar to a vector.
In list, each element is itself a data structure.
Each element of a list could be a vector, factor, matrix, dataframe or even a list itself.
lists of vectors (or) recursive vectors -- lists containing other lists.

my_list <- list(my_vector, my_matrix, my_df)
mylist <- list(team1, Stadiums, Perf_Matrix)
R_list <- list(Teaminfo = team1, St = Stadiums, Perf = Perf_Matrix )
x <- list(2.4,TRUE,1:3)
x <- list("a" = 6.5, "b" = FALSE, "c" = 1:4)
a <- list(a=1:6, b="string", c=pi, d=list(-1, -5))
empty.list <- list()
vector(mode="list", length=6)
list(1:5, letters[1:5], rnorm(5))

access a single element from list, using double brackets [[.
subsetting lists using single brackets [ results in lists.

mylist[[2]]            # Accessing second elements
mylist[["St"]]        # Accessing second elements, using column name
mylist[2]               # Accessing second element
mylist[[2]][3]        # Accessing third element in second vector/element
mylist[[1]][2,]       # Accessing second row of first data frame
list33[[4]][[2]]
> listR[c(1:2)]       # index using integer vector
> x[c(T,F,F)]         # index using logical vector
> x[-2]                 # using negative integer to exclude second component
> x[c("age","speaks")]  # index using character vector
> x["age"]            # single [ returns a list
> x[["age"]]          # double [[ returns the content
> x$name             # same as x[["name"]]
> x$a                   # partial matching, same as x$ag or x$age
> x[["a"]]             # cannot do partial match with [[
> x$speaks[1]
shining_list$reviews
shining_list[["reviews"]]
mylist[[1]][["name"]]

> length(list2)
> str(list4)
str(R_list[[4]])

> x[["name"]] <- "Satya"
> L[["married"]] <- FALSE
> L[["age"]] <- NULL
> x$married <- NULL
list3[[2]][1] = "aaaa"
song$similar$reason <- "nothingggg"
my.data[[i]] <- matrix(rnorm(16),nrow=4)
sh_list <- c(existing_column, new_element = "newwen!")

names(my_list) <- c("name1", "name2")

pairlist(…)
as.pairlist(x)
is.pairlist(list.8)

is.list(df)
is.list(lll)
as.list(x, all.names = FALSE, sorted = FALSE, …)
as.list(y ~ x1 + x2 + x3 + x4)

which.min(list(A = 7, pi = pi))

unlist(x, recursive = TRUE, use.names = TRUE)
unlist(l2)
unlist(options())
unlist(options(), use.names = FALSE)
unlist(l.ex, recursive = FALSE)
ul <- unlist(initial.param)
unlist(strsplit("a.b.c", "[.]"))
unlist(strsplit("a.b.c", ".", fixed = TRUE))

relist(flesh, skeleton = attr(flesh, "skeleton"))
relist(ul)
as.relistable(x)
is.relistable(x)

initial.param <- as.relistable(ipar)

attach(list_R)
detach(list_R)

Related Articles:   Lists in Python        Vectors in R


No comments:

Post a Comment