Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)

I have a list and I want to remove a single element from it. How can I do this?

I've tried looking up what I think the obvious names for this function would be in the reference manual and I haven't found anything appropriate.

1 Answer

0 votes
by

You can remove a particular element from a list as follows:

Mylist[[i]] <- NULL 

This will remove the ith element from the list, where i is the index of that element.

Note: This method shuffles the index of elements after removing the element.

If you don’t want to modify the list, you can use negative indexing to not include a particular element.

For example:

x <- list("A", "B", "C", "D", "E")

x[-2]

x

Output 1:

[[1]]

[1] "A"

[[2]]

[1] "C"

[[3]]

[1] "D"

[[4]]

[1] "E"

Output 2:

[[1]]

[1] "A"

[[2]]

[1] "B"

[[3]]

[1] "C"

[[4]]

[1] "D"

[[5]]

[1] "E"

Related questions

+3 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...