0

I read in a table from a pdf and it created a list with 9 lines. The lines have different character lengths so I cannot transform it into a dataframe. In line [[1]] "Region" and line [[6]] "Municipal" can both be removed and the the lines will all be 6 characters. The object is stats_lines and it is a list with [8] lines.

[[1]] [1] "Dodoma" "Region" "450305" "376924" "83.7" "92.0" "8.0"

[[2]] [1] "Kondoa" "55990" "50197" "89.7" "93.0" "7.0"

[[3]] [1] "Mpwapwa" "66275" "59670" "90.0" "93.8" "6.2"

[[4]] [1] "Kongwa" "61907" "56497" "91.3" "91.7" "8.3"

[[5]] [1] "Chamwino" "73807" "68162" "92.4" "94.4" "5.6"

[[6]] [1] "Dodoma" "Municipal" "92978" "49965" "53.7" "73.3" "26.7"

[[7]] [1] "Bahi" "49287" "45884" "93.1" "100.0" "N/A"

[[8]] [1] "Chemba" "50061" "46549" "93.0" "98.0" "2.0"

jre95
  • 13
  • 3
  • It's easier to help you if you 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 Aug 05 '20 at 20:37

1 Answers1

0

You can do this with grepl to detect the items you want to remove and then use lapply to iterate over the list, subsetting each element based on the results of grepl.

lapply(my_list function(x) x[!grepl("Region|Municipal", x)])
[[1]]
[1] "Dodoma" "450305" "376924" "83.7"   "92.0"   "8.0"   

[[2]]
[1] "Kondoa" "55990"  "50197"  "89.7"   "93.0"   "7.0"   
    
[[3]]
[1] "Mpwapwa" "66275"   "59670"   "90.0"    "93.8"    "6.2"    

[[4]]
[1] "Kongwa" "61907"  "56497"  "91.3"   "91.7"   "8.3"   

[[5]]
[1] "Chamwino" "73807"    "68162"    "92.4"     "94.4"     "5.6"     

[[6]] 
[1] "Dodoma" "92978"  "49965"  "53.7"   "73.3"   "26.7"  

[[7]]  
[1] "Bahi"  "49287" "45884" "93.1"  "100.0" "N/A"  

[[8]]
[1] "Chemba" "50061"  "46549"  "93.0"   "98.0"   "2.0" 

The str_detect function from the stringr package accomplishes the same outcome with slightly different syntax.

library(stringr)
lapply(my_list, function(x) x[!str_detect(x, "Region|Municipal")])
Ben Norris
  • 5,639
  • 2
  • 6
  • 15