0

I want to remove certain element from names.

names is a list of characters. After I run the following loop:

for (i in 1:length(names)){
  if((str_detect(names[[i]], "  Organisation Name")) || 
  (str_detect(names[[i]], "^ $")) || (str_detect(names[[i]], "^0$")) || 
  (str_detect(names[[i]], "^$"))  ){
   names[[i]] <- NULL
 }
}

I get an error. The error is:

Error in names[[i]] : subscript out of bounds

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
Ali
  • 468
  • 1
  • 8
  • 19
  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Mar 02 '18 at 21:27
  • I'm guessing that as you kill off elements of names, the "i" vector still iterates to the original length, and so you wind up grabbing an out-of-bounds element. Maybe try iterating backwards from length(names):1? Even if I'm guessing right, @MrFlick is right about reproducible examples and you should edit the question for posterity! – HarlandMason Mar 02 '18 at 21:37

2 Answers2

2

Here's some code illustrating what I think is going on based on my comment.

names <- lapply(1:5, list)
for (i in 1:length(names)) {
  names[[i]] <- NULL
  print(sprintf('Length is now %d, i is now %i', length(names), i))
  print(names[[i]])
}

This outputs

[1] "Length is now 4, i is now 1"
[[1]]
[1] 2

[1] "Length is now 3, i is now 2"
[[1]]
[1] 4

[1] "Length is now 2, i is now 3"
Error in names[[i]] : subscript out of bounds

If you iterate backwards, as with for (i in length(names):1) that might work

HarlandMason
  • 779
  • 5
  • 17
1

Since you are filtering the data, I suggest you use the built in filtering functions, such as grepl.

Combine all your regexpa into one, for better performance and compactness.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194