R List

List

  • A List is an object consisting of an ordered collection of objects known as its Components
  • The Components need not be of same type
Syntax
//Syntax:
	>list(x)

where x can be a numeric vector, a logical value, a matrix, a complex vector, a character array, a function etc…

In the example shown, the List object consists of multiple classes like:

  • numeric
  • character
CODE/PROGRAM/EXAMPLE
lst <- list(name="Tony",
	wife="Katy",
	no.children=3,
	child.ages=c(4,7,9))
	print(Lst)

	$name
	[1] "Tony"

	$wife
	[1] "Katy"

	$no.children
	[1] 3

	$child.ages
	[1] 4 7 9
  • There are 2 ways of checking, whether the variable is a list or not
Syntax
//Syntax:
	>class()
	>is.list(x)
CODE/PROGRAM/EXAMPLE
class(Lst)
	[1] "list"

	is.list(Lst)
	[1] TRUE

str() function describes the components of the object

CODE/PROGRAM/EXAMPLE
str(Lst)
output of list example in r language

as.list() converts an object to a list

CODE/PROGRAM/EXAMPLE
Std <- c("John", "kerry", "Paul", "Tom")
	newlst <- as.list(Std)
	newLst

	[[1]]
	[1] "John"

	[[2]]
	[1] "kerry"

	[[3]]
	[1] "Paul"

	[[4]]
	[1] "Tom"

	class(newLst)
	[1] "list"
Notepad

Note: Vectors can carry a single class whereas Lists can carry multiple classes

#list_in_r_programming #List_r_language #r_language_list #r_language_list_Syntax #list_example_in_r_language #list_example_in_r_programming

(New page will open, for Comment)

Not yet commented...