0 votes
1 view
in R Programming by (5.2k 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 (25.3k points)

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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...