General List Operations in R Programming

We perform various List Operations like List Indexing, Adding, Deleting List elements and accessing the List elements.

Consider the following example of list, based on which we perform some general operations

j <- list(name="Joe", salary=55000, unionms=T)

> j

$name

[1] "Joe"

$salary

[1] 55000

$unionms

[1] TRUE

You can access a list components/elements in 3 different ways:

> j$salary

[1] 55000

> j[["salary"]]

[1] 55000

> j[[2]]

[1] 55000

We can refer to list components by their numerical indices, treating the list as a vector. However, note that in this case, we use double brackets instead of single ones.

So, there are three ways to access an individual component c of a list Lst and return it in the data type of c:

Lst $cLst [["c"]]

Lst [[i]] # here i is the index of c within Lst

An alternative to the second and third techniques listed is to use single brackets rather than double brackets:

> j[1:2]

$name

[1] "Joe"

$salary

[1] 55000

> j2 <- j[2]

> j2

$salary

[1] 55000

> class(j2)

[1] "list"

> str(j2)

List of 1

$ salary: num 55000

> j2a <- j[[2]]

> j2a

[1] 55000

> class(j2a)

We can add and delete the list elements. The new components can be added after a list is created.

$a

[1] "abc"

$b

[1] 12

# adding a c component to the existing list

> z$c <- "third"

> z

$a

[1] "abc"

$b

[1] 12

$c

[1] "third"

> z[5:7] <- c(FALSE,TRUE,TRUE)

> z$a

[1] "abc"

$b

[1] 12

$c

[1] "third"

[[4]]

[1] 28

[[5]]

[1] FALSE

[[6]]

[1] TRUE

[[7]]

[1] TRUE

We can also concatenate lists as follows :

[[1]]

[1] "Joe"

[[2]]

[1] 55000

[[3]]

[1] TRUE

[[4]]

[1] 5

Getting the Size of a List

Since a list is a vector, you can obtain the number of components in a list via length().

> length(z)

[1] 4

> y <- unlist(z)

> class(y)

[1] "numeric"

> y

a b c

5 12 13

In case of Mixed mode of data, how unlist() will works is shown below example;
Where possible the list components are coerced to a common mode during the unlisting, and so the result often ends up as a character vector. Vectors will be coerced to the highest type of the components in the hierarchy NULL < raw < logical < integer < real < complex < character < list < expression: pair lists are treated as lists.

> wu

[1] "5" "xyz"

We can also remove the elements’ names directly with unname(), as follows:

[1] "5" "xyz"

[[1]]

[1] 2

[[2]]

[1] 27

Thanks, TAMATAM

We perform various List Operations like List Indexing, Adding, Deleting List elements and accessing the List elements.

Consider the following example of list, based on which we perform some general operations

j <- list(name="Joe", salary=55000, unionms=T)

> j

$name

[1] "Joe"

$salary

[1] 55000

$unionms

[1] TRUE

**1) List Indexing :**You can access a list components/elements in 3 different ways:

> j$salary

[1] 55000

> j[["salary"]]

[1] 55000

> j[[2]]

[1] 55000

We can refer to list components by their numerical indices, treating the list as a vector. However, note that in this case, we use double brackets instead of single ones.

So, there are three ways to access an individual component c of a list Lst and return it in the data type of c:

Lst $cLst [["c"]]

Lst [[i]] # here i is the index of c within Lst

An alternative to the second and third techniques listed is to use single brackets rather than double brackets:

Lst["c"]

Lst[i] # here i is the index of c within Lst

Both single-bracket and double-bracket indexing access list elements in vector-index way. But there is an important difference from ordinary (atomic) vector indexing.If single brackets [ ] are used, the result is another list--a sublist of the original.

Lst[i] # here i is the index of c within Lst

For instance, continuing to the preceding example, we can have this:

> j[1:2]

$name

[1] "Joe"

$salary

[1] 55000

> j2 <- j[2]

> j2

$salary

[1] 55000

> class(j2)

[1] "list"

> str(j2)

List of 1

$ salary: num 55000

Note:

The subsetting operation returned another list consisting of the first two components of the original list **j**. Note that the word returned makes sense here, since index brackets are functions. This is similar to other cases we’ve seen for operators that do not at first appear to be functions, such as +.

We can use double brackets [[ ]] for referencing only a single component, with the result having the type of that component.

> j[[1:2]]

Error in j[[1:2]] : subscript out of bounds

Error in j[[1:2]] : subscript out of bounds

> j2a <- j[[2]]

> j2a

[1] 55000

> class(j2a)

[1] "numeric"

**2) Adding and Deleting List Elements :**

> z <- list(a="abc",b=12)

> z$a

[1] "abc"

$b

[1] 12

# adding a c component to the existing list

> z$c <- "third"

> z

$a

[1] "abc"

$b

[1] 12

$c

[1] "third"

We can also add components via a vector index:

> z[[4]] <- 28> z[5:7] <- c(FALSE,TRUE,TRUE)

> z$a

[1] "abc"

$b

[1] 12

$c

[1] "third"

[[4]]

[1] 28

[[5]]

[1] FALSE

[[6]]

[1] TRUE

[[7]]

[1] TRUE

We can delete a list component by setting it to NULL.

> z$b <- NULL

> z$a

[1] "abc"

$c

[1] "third"

[1] "abc"

$c

[1] "third"

[[3]]

[1] 28

[[4]]

[1] FALSE

[[5]]

[1] TRUE

[[6]]

[1] TRUE

[1] 28

[[4]]

[1] FALSE

[[5]]

[1] TRUE

[[6]]

[1] TRUE

Note:

Note that upon deleting z$b, the indices of the elements after it moved up by 1. For instance , the above z[[4]] became z[[3]].We can also concatenate lists as follows :

z<-c(list("Joe", 55000, T),list(5))

[[1]]

[1] "Joe"

[[2]]

[1] 55000

[[3]]

[1] TRUE

[[4]]

[1] 5

Since a list is a vector, you can obtain the number of components in a list via length().

> length(z)

[1] 4

**3) Accessing List Components and Values :**

Consider the following list

j <- list(name="Joe", salary=55000, unionms=T)

> j

$name

[1] "Joe"

$salary

[1] 55000

$unionms

[1] TRUE

If the components in a list do have tags, as is the case with name, salary, and unionms for

The return value of unlist() is a vector—in this case, a vector of character strings. Note that the element names in this vector come from the components in the original list.j <- list(name="Joe", salary=55000, unionms=T)

> j

$name

[1] "Joe"

$salary

[1] 55000

$unionms

[1] TRUE

If the components in a list do have tags, as is the case with name, salary, and unionms for

**j**, you can obtain them via names().
> names(j)

[1] "name" "salary" "unionms"

To obtain the values, use unlist():

name salary unionms

"Joe" "55000" "TRUE"

> class(ulj)

[1] "character"

On the other hand, if we were to start with numbers, we would get numbers.The output of unlist() in this case was a numeric vector.

> z <- list(a=5,b=12,c=13)[1] "name" "salary" "unionms"

To obtain the values, use unlist():

> ulj <- unlist(j)

> uljname salary unionms

"Joe" "55000" "TRUE"

> class(ulj)

[1] "character"

On the other hand, if we were to start with numbers, we would get numbers.The output of unlist() in this case was a numeric vector.

> y <- unlist(z)

> class(y)

[1] "numeric"

> y

a b c

5 12 13

In case of Mixed mode of data, how unlist() will works is shown below example;

> w <- list(a=5,b="xyz")

> wu <- unlist(w)

> class(wu)

[1] "character"

> wu

> wu <- unlist(w)

> class(wu)

[1] "character"

> wu

a b

"5" "xyz"

Note:

Note:

Removing the List element Names :

In the above example,though wu is a vector and not a list, R did give each of the elements a name. We can remove them by setting their names to NULL.

> names(wu) <- NULL> wu

[1] "5" "xyz"

> wun <- unname(wu)

> wun[1] "5" "xyz"

**4) Using the lapply() and sapply() Functions on Lists:**
The function lapply() (for list apply) works like the matrix apply() function,calling the specified function on each component of a list (or vector coerced to a list) and returning another list. Here’s an example, where R applied median() to 1:3 and to 25:29, returning a list consisting of 2 and 27.

>lapply(list(1:3,25:29),median)[[1]]

[1] 2

[[2]]

[1] 27

Note:

In some cases, such as the example here, the list returned by lapply() could be simplified to a vector or matrix. This is exactly what sapply() (for simplified [l]apply) does.
> sapply(list(1:3,25:29),median)

[1] 2 27

[1] 2 27

## No comments:

## Post a Comment

Hi User, Thank You for Visiting My Blog. Please Post Your Feedback/Comments/Query.