0

I have the following list

n = 1:10 s = c("aa", "bb", "cc", "dd", "ee") b = c(TRUE, FALSE, TRUE, FALSE, FALSE) x = list(n, s, b, 3) 

For a vector v we can remove elements like this: v[-(1:2)]. But how do we go about removing elements from a list? Say that I want x where x[[1]] should now have the last two elements removed - is there an easy way to do this?

2
  • Writing NULL into an element effectively removes it from the list Commented Mar 21, 2017 at 17:29
  • 1
    I think your question headline could be misinterpreted as "remove elements of a list" vs. the intended "remove elements from list item that is a vector". Perhaps you could adjust your headline to make it easier for other readers to understand the question. Commented Mar 21, 2017 at 18:25

3 Answers 3

4

We can use head to remove the last two elements of the specific list element by using negative index and update the list

f1 <- function(lst, ind){ lst[[ind]] <- head(lst[[ind]], -2) lst } f1(x, 1) #[[1]] #[1] 1 2 3 4 5 6 7 8 #[[2]] #[1] "aa" "bb" "cc" "dd" "ee" #[[3]] #[1] TRUE FALSE TRUE FALSE FALSE #[[4]] #[1] 3 

Or another option using replace in the comments by @Frank

f2 <- function(lst, ind) replace(lst, ind, list(head(lst[[ind]], -2))) 
Sign up to request clarification or add additional context in comments.

5 Comments

@BillyJean That is the first case where all the list elements have the last 2 elements removed
In the OP I want the last 2 elements of the first vector of x removed
@BillyJean That is the second case I mentioned. If you want to apply to any element, then a function is better. Added that
I want the list (and all the vectors it contains) returned, but for the n'th vector the last two elements should be "shaved off".
@BillyJean Updated the post
0
 Try this C# (Just substitute your values): [Code] string[] array = new string[] { "1", "2", "3", "4", "5", "6", "7" }; List<string> list = new List<string>(array); //you can sort List<string>! list.Sort(); list.Remove("4");//remove specieifed item. list.RemoveAt(2);//remove item from index. list.RemoveRange(1, 2);//remove a range of items. [/Code] 

4 Comments

Yeah, okay, but the question is specifically about manipulating specific data structures, so using the proper language seems important. Also, it may interest you to know that "lists" in R are not linked lists.
If v[-(1:2)] removes 1, then wouldn't it be v[-2(1:2)] would remove 2?
No, I think what you're saying is not meaningful in R, where v[-(1:2)] removes the first and second elements. R is indexed starting from 1, not zero.
from what I gathered in R, this removes the 5th element: and closes up the hole. myList[[5]] <- NULL
0

Update: Avoid negative result values in the length calculation (many thanks @Frank)

To remove the last two elements of the first list item use:

x[[1]] <- x[[1]][seq_len(max(0, length(x[[1]]) - 2))] 

which results in

> x [[1]] [1] 1 2 3 4 5 6 7 8 [[2]] [1] "aa" "bb" "cc" "dd" "ee" [[3]] [1] TRUE FALSE TRUE FALSE FALSE [[4]] [1] 3 

Note that the example v[-(1:2)] of the OP does NOT remove the last two elements but the first two:

> n[-(1:2)] [1] 3 4 5 6 7 8 9 10 

If you were using names for the list items the code would even be more intuitive:

... x = list(n=n, s, b, 3) # name the first list item "n" ... x$n <- x$n[seq_len(max(0, length(x$n) - 2))] 

If the vector has less than two elements it is shortened to a length of zero.

2 Comments

1:(length(x)-y) is dangerous for reasons that I guess you know. Safer might be seq.int(max(0, length(x)-y)) or similar.
Absolutely right, not assuming any precondition is a must. I will change it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.