9

Among a few less relevant others, I checked these two answers:
Answer 1
Answer 2

However, the solutions presented there did not help.

I am probably misunderstanding my own problem and trying to do the right thing the wrong way. I appreciate any help.

I have the following code where I build a list of strings and try to delete the second element of the list:

> my_strings <- "string1 string2 string3 string4 string5"
> my_list <- strsplit(my_strings,split=" ")
> #Now trying to delete one element from my_list using positive indexing
>
> my_list[[2]] <- NULL #does not work
> my_list[2] <- NULL #nope. Doesn't work either
> my_list[[1]][2] <- NULL #error: replacement has length zero
> my_list[[1]][[2]] <- NULL # error: more elements supplied than there are to replace

So, my question is: how can I delete the second element (or multiple elements, like 1 and 3) of my_list? The elements of my_list are not named, I want to access them by the numeric index.

Community
  • 1
  • 1
bomgaroto
  • 95
  • 1
  • 1
  • 6
  • 6
    `mylist[[1]] <- mylist[[1]][-2]`. I'd suggest reading a bit on the way subsetting and lists work in R. – Justin Jul 16 '13 at 18:18
  • 2
    Take-home message: Use subsetting instead of assigning `NULL`. – Roland Jul 16 '13 at 18:21
  • Thank you very much Justin (and Roland). Using the line you showed with a range instead of the negative index works perfectly for my purposes. I was trying to remove the items with the "<- NULL" construct, but it does not work. Subsetting and then overwriting the list works perfectly and the result is the same. So I guess I cannot simply remove elements, but I rather have to subset the list and replace it with the smaller list – bomgaroto Jul 16 '13 at 18:26
  • Subsetting sure works great to work on a list of strings. You can use ranges either with positive indices (`mylist[[1]][3:7]` keeps elements 3 to 7) or negative indices (`mylist[[1]][-3:-7]` discards elements 3 to 7). Thank you again! – bomgaroto Jul 16 '13 at 19:23
  • Update: Variables for range of indices DO work, but you have to guarantee that 'both' indices are either negative or positive. For example, `mylist[[1]][var1:var2]` will work only if both indices are either positive or negative. – bomgaroto Jul 16 '13 at 22:24

1 Answers1

4

I'm not sure you intended to create a list of vectors with your code; might be easier to just use a character vector. Try using unlist first:

my_list <- unlist(strsplit(my_strings,split=" "))

my_list <- my_list[-2]
canary_in_the_data_mine
  • 2,193
  • 2
  • 24
  • 28
  • Ah, okay. Than @Justin's answer is best. – canary_in_the_data_mine Jul 16 '13 at 18:34
  • Your answer works fine, but I have to use list() again in the second line, otherwise I end up with something that is not a list. However, the subsetting option presented by Justin and Roland caught my eye better. I accepted your answer though, since they only added comments. – bomgaroto Jul 16 '13 at 18:39
  • Yup, I misspoke -- sorry for the confusion! But again, sounds like @Justin's solution took care of your needs. – canary_in_the_data_mine Jul 16 '13 at 19:08