0

I have a character column of multiplygon type where each value has a string containing multiple lat long coordinates,i want to find all the rows having lat long less than 4 along with the values.I have done this using for loops in R but that makes the execution very slow. Is there a better way to achieve this.I have tried using str.split with lapply and rbind but as the column number changes with each str.split the rbind fills the columns automatically instead of leaving NAs.Below is the sample code that is working fine with for loops but but with large data it gets very slow. 'test3' is the dataframe and 'location' is the column containing multiple lat long values(each column value is of different length) Sample column value:

1660|ABC|Urban|const|ABC|1660|ABC-23_Girvan|MultiPolygon (((-4.8066907 55.04347151, -4.80276148 55.04454913, -4.80273576 55.04455619, -4.80272906 55.0445397, -4.8019362 55.04259356, -4.80144005 55.04137563, -4.80143642 55.04136669, -4.80143318 55.04136487, -4.79932797 55.04017957, -4.79603597 55.03832586, -4.79602477 55.03831956, -4.79602909 55.03830962, -4.79681797 55.03648828, -4.79736311 55.0352296, -4.79736682 55.03522103, -4.79737054 55.03521246, -4.79734694 55.03519212, -4.79442154 55.03266764, -4.79436702 55.03262058, -4.79429576 55.032635, -4.79012226 55.03347964, -4.79007062 55.03349009, -4.78999373 55.03356092, -4.78954942 55.03397026, -4.78945452 55.03396611, -4.78523126 55.03378099, -4.78474862 55.03585432, -4.78410562 55.03755613, -4.78131992 55.03769417, -4.77995653 55.03933729, -4.7781591 55.04093226, -4.77183228 55.03970088, -4.77169854 55.03967882, -4.76459029 55.03850547, -4.76218988 55.03966093, -4.76218404 55.03966375, -4.76216777 55.03967074, -4.76208001 55.03970845, -4.75859975 55.03906771, -4.7524626 55.04284217, -4.7463334 55.04429135, -4.74314492 55.04672472, -4.74144307 55.04822486, -4.74077155 55.04822731, -4.74042671 55.04785351, -4.73920006 55.04652378, -4.73823528 55.04516571, -4.73819305 55.04511062, -4.73818743 55.04510704, -4.73738004 55.04459257, -4.73737984 55.04459012, -4.73729138 55.04348033, -4.73404678 55.0427649, -4.73162755 55.04424351, -4.72684084 55.04622438, -4.72454247 55.0462497, -4.72419217 55.04606356, -4.72258998 55.04538684, -4.72258924 55.04538653, -4.72258871 55.04538461, -4.72235393 55.04454127, -4.72220504 55.04400487, -4.72219207 55.04395817, -4.72215619 55.04393533, -4.72058722 55.04293649, -4.72057629 55.04292953, -4.72024656 55.04287095, -4.72024615 55.04287087, -4.72024571 55.0428708, -4.71960906 55.04275791, -4.71960096 55.04274676, -4.71872616 55.04154264, -4.71859094 55.04135649, -4.71824434 55.04082598, -4.71821688 55.04077155, -4.71791095 55.0401652, -4.71788206 55.04011276, -4.71787802 55.04010957, -4.71722657 55.03959414, -4.71540401 55.03999767, -4.71538959 55.04000086, -4.71487796 55.04025596, -4.71443494 55.03989911, -4.6918896 55.04502024, -4.69187352 55.04502389, -4.68493194 55.04330826, -4.68485827 55.04330196, -4.68484986 55.04331181, -4.68281241 55.04569817, -4.68274628 55.04570207, -4.67796529 55.04598422, -4.67578179 55.04884382, -4.67396727 55.05046258, -4.67239289 55.05062117, -4.67207876 55.05062863, -4.67145075 55.05064354, -4.67135174 55.05067477, -4.67083231 55.0508386, -4.67050812 55.05090876, -4.67020746 55.05097384, -4.66989921 55.05104055, -4.6692961 55.05105565, -4.6692866 55.05105588, -4.66928375 55.05105675, -4.66870015 55.05123527, -4.66865176 55.05125052, -4.66847284 55.05141605, -4.66821431 55.0516552, -4.6598276 55.0539769, -4.65904234 55.05348943, -4.65899383 55.05345932, -4.65895686 55.05347453, -4.65713805 55.0542235, -4.65541219 55.05510604, -4.64918418 55.05654281, -4.64529045 55.05746473, -4.64472591 55.05740647, -4.64454119 55.05540513, -4.64597245 55.05372462, -4.64620866 55.05173229, -4.64481074 55.0506421, -4.6444775 55.05038221, -4.64067873 55.05173855, -4.63732431 55.05211035, -4.63358558 55.05256525, -4.63336187 55.05388239, -4.63337637 55.05575676, -4.6329212 55.05646996, -4.63148884 55.05659632, -4.62928993 55.05736526, -4.6273795 55.05758632, -4.62386505 55.05783972, -4.62063736 55.0575441, -4.61845101 55.05867326, -4.61688535 55.05942835, -4.61469633 55.06055804, -4.61215512 55.06115143, -4.6086278 55.06140072, -4.61068485 55.06371569, -4.6145987 55.06472496, -4.6178753 55.06592646, -4.62214499 55.06783014, -4.62384362 55.06978576, -4.62435655 55.07070437, -4.62308292 55.07251163, -4.62535069 55.07251111, -4.62665101 55.07515748, -4.62832449 55.07617534, -4.62740353 55.07789383, -4.63292986 55.08032549, -4.63636727 55.07929898, -4.63674717 55.08035457, -4.63597826 55.08579344, -4.63208926 55.09491179, -4.62966443 55.10145779, -4.62555688 55.12333185, -4.62453006 55.1264075, -4.62308495 55.12787298, -4.61804869 55.13120733, -4.6118031 55.13473534, -4.60906361 55.13760085, -4.60737243 55.14124439, -4.60729076 55.14142037, -4.60728565 55.14308823, -4.60728565 55.14308823, -4.60288497 55.14207939, -4.59554115 55.14069688, -4.59311992 55.14314738, -4.58945406 55.14296188, -4.58688111 55.14342821, -4.58565648 55.14532862, -4.58217713 55.14502652, -4.57473495 55.14578197, -4.57715915 55.14850591, -4.57255628 55.15058907, -4.57241801 55.15306662, -4.57060383 55.1575286, -4.56503363 55.15633224, -4.56497722 55.15817053, -4.56450719 55.15971754, -4.56253598 55.16039597, -4.55886596 55.15930976, -4.55401746 55.16075999, -4.54797659 55.16110981, -4.5408484 55.16088173, -4.5369834 55.15979893, -4.53252787 55.16054235, -4.53143396 55.16070717, -4.51799229 55.15707571, -4.51271254 55.15362149, -4.50458987 55.15491117, -4.49940285 55.1544539, -4.47472464 55.15592548, -4.47382559 55.15789315, -4.47166113 55.15951106, -4.46809419 55.15890751, -4.46608368 55.15995999, -4.46179496 55.16375731, -4.45882785 55.16869028, -4.45995621 55.17162983, -4.45685079 55.17289131, -4.45143386 55.17198661, -4.44301984 55.1702411, -4.44306281 55.16724087, -4.44288324 55.16476994, -4.4446801 55.16345964, -4.44554009 55.16183049, -4.44485583 55.16049424, -4.43952776 55.15808756, -4.43976355 55.1496472, -4.43755762 55.14335338, -4.43805226 55.14283349, -4.57473486 55.14578185, -4.60588908 55.07343634, -4.59519957 55.06233673, -4.60013217 55.06077222, -4.58944762 55.04967193, -4.58979889 55.04956051, -4.60742485 55.0511097, -4.60742485 55.05110969, -4.60742504 55.0511097, -4.60751859 55.05075858, -4.60818492 55.05109407, -4.608185 55.05109402, -4.60818511 55.05109407, -4.62479165 55.04021827, -4.62507206 55.04295189, -4.62507226 55.04295188, -4.62507226 55.0429519, -4.63288233 55.04268746, -4.63071659 55.02160371, -4.64992574 55.04154173, -4.64992589 55.04154168, -4.64992594 55.04154173, -4.65485366 55.03997507, -4.65572824 55.04088217, -4.65572834 55.04088214, -4.65572838 55.04088218, -4.66257217 55.03870582, -4.66055361 55.0366125, -4.66548053 55.03504536, -4.66346206 55.03295215, -4.66838841 55.03138489, -4.65562524 55.01814485, -4.66790927 55.01520004, -4.66595911 55.01785411, -4.72205117 55.03144487, -4.72205119 55.03144484, -4.72205135 55.03144487, -4.72508752 55.02730471, -4.72508752 55.02730471, -4.72727318 55.02432375, -4.83764211 54.99347404, -4.8391614 54.99347765, -4.84818311 54.99349909, -4.8538311 54.99932518, -4.80669355 55.04346885, -4.8066907 55.04347151))|Pnt (-4.65723857 55.07320805)|||
1552|ABC|Urban|const|ABC|1552|ABC-23_Girvan|MultiPolygon (((-4.27763349 55.3106471, -4.27809894 55.31053055, -4.28715259 55.30983888, -4.29059196 55.31033773, -4.29705406 55.31287996, -4.29825773 55.31022995, -4.29915871 55.31080432, -4.3040445 55.31063359, -4.32196403 55.30841057, -4.35013465 55.30337916, -4.35608607 55.30381051, -4.36029362 55.30259222, -4.36348063 55.30099468, -4.36629835 55.29901995, -4.36777166 55.29666621, -4.36854792 55.29393497, -4.36859387 55.29046006, -4.36444536 55.27604388, -4.36310057 55.27124315, -4.36296541 55.27076095, -4.36290831 55.27021376, -4.36010948 55.26458948, -4.35921514 55.2627564, -4.35605039 55.25626822, -4.35456341 55.25080791, -4.35539054 55.24894911, -4.35797725 55.24485067, -4.35873852 55.24082456, -4.3592542 55.23702803, -4.36225353 55.23415897, -4.36549236 55.23098519, -4.36801163 55.22688783, -4.37341748 55.221123, -4.37385869 55.21721542, -4.37523926 55.21366483, -4.3793209 55.2104371, -4.38160336 55.20671897, -4.3877229 55.204614, -4.38954584 55.20262034, -4.39093331 55.20110279, -4.39231314 55.19665216, -4.39260793 55.19589522, -4.39380369 55.19564477, -4.39652619 55.19507449, -4.40571406 55.19426214, -4.41280656 55.19434166, -4.41869148 55.19478236, -4.42677958 55.19588568, -4.43924456 55.19779309, -4.44834509 55.19939003, -4.45205979 55.20004164, -4.46744208 55.20312624, -4.46992825 55.20353096, -4.46926582 55.20713926, -4.46942315 55.20942647, -4.4696447 55.21260001, -4.47045278 55.21524371, -4.4715235 55.21718096, -4.47293288 55.21947482, -4.47572079 55.22354572, -4.47812634 55.22656423, -4.48298204 55.23604339, -4.48339424 55.23712038, -4.4835154 55.23838193, -4.48391374 55.23927942, -4.48464117 55.24035335, -4.48664623 55.24177203, -4.48803303 55.24319935, -4.48880194 55.24463532, -4.48814179 55.24717571, -4.48656682 55.24972967, -4.48588827 55.25191147, -4.48554678 55.25427066, -4.48464194 55.25700088, -4.48341866 55.25955612, -4.48126257 55.26194436, -4.47822172 55.26452863, -4.47488634 55.26711767, -4.47115682 55.26898763, -4.47084436 55.26910913, -4.46970303 55.26955302, -4.46682164 55.27086635, -4.46490688 55.27252975, -4.46388379 55.27399928, -4.46402284 55.27727229, -4.46597052 55.28033742, -4.46811244 55.28248736, -4.47054703 55.28444876, -4.47407458 55.28747954, -4.47681134 55.28924873, -4.48081946 55.29117462, -4.48491314 55.29364022, -4.49071678 55.29715731, -4.49389421 55.29981433, -4.49578891 55.30231379, -4.49675655 55.30501139, -4.49776705 55.30825135, -4.49833939 55.31059465, -4.50014046 55.31291222, -4.50202214 55.31631459, -4.50542276 55.31968523, -4.51009609 55.32393858, -4.51441426 55.32820807, -4.51834953 55.33176287, -4.52164867 55.33551663, -4.52584909 55.33870505, -4.52849713 55.3424766, -4.53244048 55.34767851, -4.53507095 55.35126714, -4.53535285 55.35164433, -4.53868897 55.3561076, -4.54165075 55.35986615, -4.54364242 55.36273418, -4.54404772 55.3654532, -4.54421522 55.3659913, -4.54511516 55.36888201, -4.54602098 55.3699382, -4.52896118 55.37263801, -4.50087758 55.35824432, -4.47661559 55.37139693, -4.44762781 55.38682775, -4.32023047 55.33238024, -4.27301552 55.31545439, -4.27763349 55.3106471)))|Point (-4.42368856 55.2960241)|||
1625|ABC|Urban|const|ABC|1625|ABC-23_Girvan|MultiPolygon (((-4.6 55.07929898, -4.63292986 55.08032549, -4.62740353 55.07789383, -4.62832449 55.07617534, -4.62894805 55.07655459, -4.63067565 55.07719054, -4.63218879 55.07774751, -4.63603734 55.0783822, -4.63636727 55.07929898)))|Point (-4.63175565 55.0784619)|||



And so on..... My expected output for 'location'should be a dataframe with all the lat long values that are less than 4 like

     col1   col2  col3 col4 col5 col6 col7 col8
Row1: -5.233 -5.679 NA -6.345 NA
Row2: -5.285 -5.888 -5.2 -5.0 NA -5.7 NA -5.888 and so on
test3<- data.frame(lapply(test3, function(x) {gsub("[A-Z]", "", x )}))
  test3<- data.frame(lapply(test3, function(x) {gsub("[a-z]", "", x )}))
  test3<- data.frame(lapply(test3, function(x) {gsub("[(]", "", x )}))
  test3<- data.frame(lapply(test3, function(x) {gsub("[)]", "", x )}))
  test3<- data.frame(lapply(test3, function(x) {gsub(", ", " ", x )}))
  #test3<- data.frame(lapply(test3, function(x) {gsub(",", " ", x )}))
  test3 <- data.frame(lapply(test3, trimws), stringsAsFactors = FALSE)
  datt <- data.frame()
  for (k in 1:nrow(test3)){
    test4<-data.frame(do.call("rbind", strsplit(as.character(test3[k,"Location"]), " ", fixed = TRUE)))
    for (i in colnames(test4)){
      if( nchar(strsplit(as.character(test4[[i]]), "\\.")[[1]][2])<4 || is.na(nchar(strsplit(as.character(test4[[i]]), "\\.")[[1]][2]))==TRUE){
        datt[k,i] <- data.frame(test4[[i]])
        next
      }
     
     
    }
  }
 
  d_chk_loc<- datt[rowSums(is.na(datt)) != ncol(datt), ]

dput output

structure(list(Id = c(1660L, 1552L, 1625L), Coverage = c("MultiPolygon (((-4.8066907 55.04347151, -4.80276148 55.04454913, -4.80273576 55.04455619, -4.80272906 55.0445397, -4.8019362 55.04259356, -4.80144005 55.04137563, -4.80143642 55.04136669, -4.80143318 55.04136487, -4.79932797 55.04017957, -4.79603597 55.03832586, -4.79602477 55.03831956, -4.79602909 55.03830962, -4.79681797 55.03648828, -4.79736311 55.0352296, -4.79736682 55.03522103, -4.79737054 55.03521246, -4.79734694 55.03519212, -4.79442154 55.03266764, -4.79436702 55.03262058, -4.79429576 55.032635, -4.79012226 55.03347964, -4.79007062 55.03349009, -4.78999373 55.03356092, -4.78954942 55.03397026, -4.78945452 55.03396611, -4.78523126 55.03378099, -4.78474862 55.03585432, -4.78410562 55.03755613, -4.78131992 55.03769417, -4.77995653 55.03933729, -4.7781591 55.04093226, -4.77183228 55.03970088, -4.77169854 55.03967882, -4.76459029 55.03850547, -4.76218988 55.03966093, -4.76218404 55.03966375, -4.76216777 55.03967074, -4.76208001 55.03970845, -4.75859975 55.03906771, -4.7524626 55.04284217, -4.7463334 55.04429135, -4.74314492 55.04672472, -4.74144307 55.04822486, -4.74077155 55.04822731, -4.74042671 55.04785351, -4.73920006 55.04652378, -4.73823528 55.04516571, -4.73819305 55.04511062, -4.73818743 55.04510704, -4.73738004 55.04459257, -4.73737984 55.04459012, -4.73729138 55.04348033, -4.73404678 55.0427649, -4.73162755 55.04424351, -4.72684084 55.04622438, -4.72454247 55.0462497, -4.72419217 55.04606356, -4.72258998 55.04538684, -4.72258924 55.04538653, -4.72258871 55.04538461, -4.72235393 55.04454127, -4.72220504 55.04400487, -4.72219207 55.04395817, -4.72215619 55.04393533, -4.72058722 55.04293649, -4.72057629 55.04292953, -4.72024656 55.04287095, -4.72024615 55.04287087, -4.72024571 55.0428708, -4.71960906 55.04275791, -4.71960096 55.04274676, -4.71872616 55.04154264, -4.71859094 55.04135649, -4.71824434 55.04082598, -4.71821688 55.04077155, -4.71791095 55.0401652, -4.71788206 55.04011276, -4.71787802 55.04010957, -4.71722657 55.03959414, -4.71540401 55.03999767, -4.71538959 55.04000086, -4.71487796 55.04025596, -4.71443494 55.03989911, -4.6918896 55.04502024, -4.69187352 55.04502389, -4.68493194 55.04330826, -4.68485827 55.04330196, -4.68484986 55.04331181, -4.68281241 55.04569817, -4.68274628 55.04570207, -4.67796529 55.04598422, -4.67578179 55.04884382, -4.67396727 55.05046258, -4.67239289 55.05062117, -4.67207876 55.05062863, -4.67145075 55.05064354, -4.67135174 55.05067477, -4.67083231 55.0508386, -4.67050812 55.05090876, -4.67020746 55.05097384, -4.66989921 55.05104055, -4.6692961 55.05105565, -4.6692866 55.05105588, -4.66928375 55.05105675, -4.66870015 55.05123527, -4.66865176 55.05125052, -4.66847284 55.05141605, -4.66821431 55.0516552, -4.6598276 55.0539769, -4.65904234 55.05348943, -4.65899383 55.05345932, -4.65895686 55.05347453, -4.65713805 55.0542235, -4.65541219 55.05510604, -4.64918418 55.05654281, -4.64529045 55.05746473, -4.64472591 55.05740647, -4.64454119 55.05540513, -4.64597245 55.05372462, -4.64620866 55.05173229, -4.64481074 55.0506421, -4.6444775 55.05038221, -4.64067873 55.05173855, -4.63732431 55.05211035, -4.63358558 55.05256525, -4.63336187 55.05388239, -4.63337637 55.05575676, -4.6329212 55.05646996, -4.63148884 55.05659632, -4.62928993 55.05736526, -4.6273795 55.05758632, -4.62386505 55.05783972, -4.62063736 55.0575441, -4.61845101 55.05867326, -4.61688535 55.05942835, -4.61469633 55.06055804, -4.61215512 55.06115143, -4.6086278 55.06140072, -4.61068485 55.06371569, -4.6145987 55.06472496, -4.6178753 55.06592646, -4.62214499 55.06783014, -4.62384362 55.06978576, -4.62435655 55.07070437, -4.62308292 55.07251163, -4.62535069 55.07251111, -4.62665101 55.07515748, -4.62832449 55.07617534, -4.62740353 55.07789383, -4.63292986 55.08032549, -4.63636727 55.07929898, -4.63674717 55.08035457, -4.63597826 55.08579344, -4.63208926 55.09491179, -4.62966443 55.10145779, -4.62555688 55.12333185, -4.62453006 55.1264075, -4.62308495 55.12787298, -4.61804869 55.13120733, -4.6118031 55.13473534, -4.60906361 55.13760085, -4.60737243 55.14124439, -4.60729076 55.14142037, -4.60728565 55.14308823, -4.60728565 55.14308823, -4.60288497 55.14207939, -4.59554115 55.14069688, -4.59311992 55.14314738, -4.58945406 55.14296188, -4.58688111 55.14342821, -4.58565648 55.14532862, -4.58217713 55.14502652, -4.57473495 55.14578197, -4.57715915 55.14850591, -4.57255628 55.15058907, -4.57241801 55.15306662, -4.57060383 55.1575286, -4.56503363 55.15633224, -4.56497722 55.15817053, -4.56450719 55.15971754, -4.56253598 55.16039597, -4.55886596 55.15930976, -4.55401746 55.16075999, -4.54797659 55.16110981, -4.5408484 55.16088173, -4.5369834 55.15979893, -4.53252787 55.16054235, -4.53143396 55.16070717, -4.51799229 55.15707571, -4.51271254 55.15362149, -4.50458987 55.15491117, -4.49940285 55.1544539, -4.47472464 55.15592548, -4.47382559 55.15789315, -4.47166113 55.15951106, -4.46809419 55.15890751, -4.46608368 55.15995999, -4.46179496 55.16375731, -4.45882785 55.16869028, -4.45995621 55.17162983, -4.45685079 55.17289131, -4.45143386 55.17198661, -4.44301984 55.1702411, -4.44306281 55.16724087, -4.44288324 55.16476994, -4.4446801 55.16345964, -4.44554009 55.16183049, -4.44485583 55.16049424, -4.43952776 55.15808756, -4.43976355 55.1496472, -4.43755762 55.14335338, -4.43805226 55.14283349, -4.57473486 55.14578185, -4.60588908 55.07343634, -4.59519957 55.06233673, -4.60013217 55.06077222, -4.58944762 55.04967193, -4.58979889 55.04956051, -4.60742485 55.0511097, -4.60742485 55.05110969, -4.60742504 55.0511097, -4.60751859 55.05075858, -4.60818492 55.05109407, -4.608185 55.05109402, -4.60818511 55.05109407, -4.62479165 55.04021827, -4.62507206 55.04295189, -4.62507226 55.04295188, -4.62507226 55.0429519, -4.63288233 55.04268746, -4.63071659 55.02160371, -4.64992574 55.04154173, -4.64992589 55.04154168, -4.64992594 55.04154173, -4.65485366 55.03997507, -4.65572824 55.04088217, -4.65572834 55.04088214, -4.65572838 55.04088218, -4.66257217 55.03870582, -4.66055361 55.0366125, -4.66548053 55.03504536, -4.66346206 55.03295215, -4.66838841 55.03138489, -4.65562524 55.01814485, -4.66790927 55.01520004, -4.66595911 55.01785411, -4.72205117 55.03144487, -4.72205119 55.03144484, -4.72205135 55.03144487, -4.72508752 55.02730471, -4.72508752 55.02730471, -4.72727318 55.02432375, -4.83764211 54.99347404, -4.8391614 54.99347765, -4.84818311 54.99349909, -4.8538311 54.99932518, -4.80669355 55.04346885, -4.8066907 55.04347151))", 
"MultiPolygon (((-4.27763349 55.3106471, -4.27809894 55.31053055, -4.28715259 55.30983888, -4.29059196 55.31033773, -4.29705406 55.31287996, -4.29825773 55.31022995, -4.29915871 55.31080432, -4.3040445 55.31063359, -4.32196403 55.30841057, -4.35013465 55.30337916, -4.35608607 55.30381051, -4.36029362 55.30259222, -4.36348063 55.30099468, -4.36629835 55.29901995, -4.36777166 55.29666621, -4.36854792 55.29393497, -4.36859387 55.29046006, -4.36444536 55.27604388, -4.36310057 55.27124315, -4.36296541 55.27076095, -4.36290831 55.27021376, -4.36010948 55.26458948, -4.35921514 55.2627564, -4.35605039 55.25626822, -4.35456341 55.25080791, -4.35539054 55.24894911, -4.35797725 55.24485067, -4.35873852 55.24082456, -4.3592542 55.23702803, -4.36225353 55.23415897, -4.36549236 55.23098519, -4.36801163 55.22688783, -4.37341748 55.221123, -4.37385869 55.21721542, -4.37523926 55.21366483, -4.3793209 55.2104371, -4.38160336 55.20671897, -4.3877229 55.204614, -4.38954584 55.20262034, -4.39093331 55.20110279, -4.39231314 55.19665216, -4.39260793 55.19589522, -4.39380369 55.19564477, -4.39652619 55.19507449, -4.40571406 55.19426214, -4.41280656 55.19434166, -4.41869148 55.19478236, -4.42677958 55.19588568, -4.43924456 55.19779309, -4.44834509 55.19939003, -4.45205979 55.20004164, -4.46744208 55.20312624, -4.46992825 55.20353096, -4.46926582 55.20713926, -4.46942315 55.20942647, -4.4696447 55.21260001, -4.47045278 55.21524371, -4.4715235 55.21718096, -4.47293288 55.21947482, -4.47572079 55.22354572, -4.47812634 55.22656423, -4.48298204 55.23604339, -4.48339424 55.23712038, -4.4835154 55.23838193, -4.48391374 55.23927942, -4.48464117 55.24035335, -4.48664623 55.24177203, -4.48803303 55.24319935, -4.48880194 55.24463532, -4.48814179 55.24717571, -4.48656682 55.24972967, -4.48588827 55.25191147, -4.48554678 55.25427066, -4.48464194 55.25700088, -4.48341866 55.25955612, -4.48126257 55.26194436, -4.47822172 55.26452863, -4.47488634 55.26711767, -4.47115682 55.26898763, -4.47084436 55.26910913, -4.46970303 55.26955302, -4.46682164 55.27086635, -4.46490688 55.27252975, -4.46388379 55.27399928, -4.46402284 55.27727229, -4.46597052 55.28033742, -4.46811244 55.28248736, -4.47054703 55.28444876, -4.47407458 55.28747954, -4.47681134 55.28924873, -4.48081946 55.29117462, -4.48491314 55.29364022, -4.49071678 55.29715731, -4.49389421 55.29981433, -4.49578891 55.30231379, -4.49675655 55.30501139, -4.49776705 55.30825135, -4.49833939 55.31059465, -4.50014046 55.31291222, -4.50202214 55.31631459, -4.50542276 55.31968523, -4.51009609 55.32393858, -4.51441426 55.32820807, -4.51834953 55.33176287, -4.52164867 55.33551663, -4.52584909 55.33870505, -4.52849713 55.3424766, -4.53244048 55.34767851, -4.53507095 55.35126714, -4.53535285 55.35164433, -4.53868897 55.3561076, -4.54165075 55.35986615, -4.54364242 55.36273418, -4.54404772 55.3654532, -4.54421522 55.3659913, -4.54511516 55.36888201, -4.54602098 55.3699382, -4.52896118 55.37263801, -4.50087758 55.35824432, -4.47661559 55.37139693, -4.44762781 55.38682775, -4.32023047 55.33238024, -4.27301552 55.31545439, -4.27763349 55.3106471)))", 
"MultiPolygon (((-4.6 55.07929898, -4.63292986 55.08032549, -4.62740353 55.07789383, -4.62832449 55.07617534, -4.62894805 55.07655459, -4.63067565 55.07719054, -4.63218879 55.07774751, -4.63603734 55.0783822, -4.63636727 55.07929898)))"
)), row.names = c(NA, 3L), class = "data.frame")
>     View(MA_in)
> 
user1820133
  • 41
  • 1
  • 4

2 Answers2

0
library(tidyverse)

data <- 
  c("MultiPolygon (((-4.806 55.04347151, -4.80 55.04454913, -4.80273576 55.04455619, -4.80272906 55.0445397, -4.8019362 55.04259356, -4.80144005 55.04137563, -4.80143642 55.04136669, -4.80143318 55.04136487, -4.799 55.04017957, -4.79603597 55.03832586, -4.79602477 55.03831956, -4.79602909 55.03830962, -4.79681797 55.03648828, -4.79736311 55.0352296, -4.79736682 55.03522103, -4.79737054 55.035, -4.79734694 55.03519212, -4.79442154 55.03266764, -4.79436702 55.03262058, -4.79429576 55.032635, -4.79012226 55.03347964, -4.79007062 55.03349009, -4.78999373 55.03356092, -4.78954942 55.03397026, -4.78945452 55.03396611, -4.78523126 55.03378099, -4.78474862 55.03585432, -4.78410562 55.03755613, -4.78131992 55.03769417, -4.77995653 55.03933729, -4.7781591 55.04093226, -4.77183228 55.03970088, -4.77169854 55.03967882, -4.76459029 55.03850547, -4.76218988 55.03966093, -4.76218404 55.03966375, -4.76216777 55.03967074, -4.76208001 55.03970845, -4.75859975 55.03906771, -4.7524626 55.04284217, -4.7463334 55.04429135, -4.74314492 55.04672472, -4.74144307 55.04822486, -4.74077155 55.04822731, -4.74042671 55.04785351, -4.73920006 55.04652378, -4.73823528 55.04516571, -4.73819305 55.04511062, -4.73818743 55.04510704, -4.73738004 55.04459257, -4.73737984 55.04459012, -4.73729138 55.04348033, -4.73404678 55.0427649, -4.73162755 55.04424351, -4.72684084 55.04622438, -4.72454247 55.0462497, -4.72419217 55.04606356, -4.72258998 55.04538684, -4.72258924 55.04538653, -4.72258871 55.04538461, -4.72235393 55.04454127, -4.72220504 55.04400487, -4.72219207 55.04395817, -4.72215619 55.04393533, -4.72058722 55.04293649, -4.72057629 55.04292953, -4.72024656 55.04287095, -4.72024615 55.04287087, -4.72024571 55.0428708, -4.71960906 55.04275791, -4.71960096 55.04274676, -4.71872616 55.04154264, -4.71859094 55.04135649, -4.71824434 55.04082598, -4.71821688 55.04077155, -4.71791095 55.0401652, -4.71788206 55.04011276, -4.71787802 55.04010957, -4.71722657 55.03959414, -4.71540401 55.03999767, -4.71538959 55.04000086, -4.71487796 55.04025596, -4.71443494 55.03989911, -4.6918896 55.04502024, -4.69187352 55.04502389, -4.68493194 55.04330826, -4.68485827 55.04330196, -4.68484986 55.04331181, -4.68281241 55.04569817, -4.68274628 55.04570207, -4.67796529 55.04598422, -4.67578179 55.04884382, -4.67396727 55.05046258, -4.67239289 55.05062117, -4.67207876 55.05062863, -4.67145075 55.05064354, -4.67135174 55.05067477, -4.67083231 55.0508386, -4.67050812 55.05090876, -4.67020746 55.05097384, -4.66989921 55.05104055, -4.6692961 55.05105565, -4.6692866 55.05105588, -4.66928375 55.05105675, -4.66870015 55.05123527, -4.66865176 55.05125052, -4.66847284 55.05141605)))",
    "MultiPolygon (((-4.27763349 55.3106471, -4.27809894 55.31053055, -4.28715259 55.30983888, -4.29059196 55.31033773, -4.29705406 55.31287996, -4.29825773 55.31022995, -4.29915871 55.31080432, -4.3040445 55.31063359, -4.32196403 55.30841057, -4.35013465 55.30337916, -4.35608607 55.3, -4.36029362 55.30259222, -4.36348063 55.30099468, -4.36629835 55.29901995, -4.36777166 55.29666621, -4.36854792 55.29393497, -4.36859387 55.29046006, -4.36444536 55.27604388, -4.36310057 55.27124315, -4.36296541 55.27076095, -4.36290831 55.27021376, -4.36010948 55.26458948, -4.35921514 55.2627564, -4.35605039 55.25626822, -4.35456341 55.25080791, -4.35539054 55.24894911, -4.35797725 55.24485067, -4.35873852 55.24082456, -4.3592542 55.23702803, -4.36225353 55.23415897, -4.36549236 55.23098519, -4.36801163 55.22688783, -4.37341748 55.221123, -4.37385869 55.21721542, -4.37523926 55.21366483, -4.3793209 55.2104371, -4.38160336 55.20671897, -4.3877229 55.204614, -4.38954584 55.20262034, -4.39093331 55.20110279, -4.39231314 55.19665216, -4.39260793 55.19589522, -4.39380369 55.19564477, -4.39652619 55.19507449, -4.40571406 55.19426214, -4.41280656 55.19434166, -4.41869148 55.19478236, -4.42677958 55.19588568, -4.43924456 55.19779309, -4.44834509 55.19939003, -4.45205979 55.20004164, -4.46744208 55.20312624, -4.46992825 55.20353096, -4.46926582 55.20713926, -4.46942315 55.20942647, -4.4696447 55.21260001, -4.47045278 55.21524371, -4.4715235 55.21718096, -4.47293288 55.21947482, -4.47572079 55.22354572, -4.47812634 55.22656423, -4.48298204 55.23604339, -4.48339424 55.23712038, -4.4835154 55.23838193, -4.48391374 55.23927942, -4.48464117 55.24035335, -4.48664623 55.24177203, -4.48803303 55.24319935, -4.48880194 55.24463532, -4.48814179 55.24717571, -4.48656682 55.24972967, -4.48588827 55.25191147, -4.48554678 55.25427066, -4.48464194 55.25700088, -4.48341866 55.25955612, -4.48126257 55.26194436, -4.47822172 55.26452863, -4.47488634 55.26711767, -4.47115682 55.26898763, -4.47084436 55.26910913, -4.46970303 55.26955302, -4.46682164 55.27086635, -4.46490688 55.27252975, -4.46388379 55.27399928, -4.46402284 55.27727229, -4.46597052 55.28033742, -4.46811244 55.28248736, -4.47054703 55.28444876, -4.47407458 55.28747954, -4.47681134 55.28924873, -4.48081946 55.29117462, -4.48491314 55.29364022, -4.49071678 55.29715731, -4.49389421 55.29981433, -4.49578891 55.30231379, -4.49675655 55.30501139, -4.49776705 55.30825135, -4.49833939 55.31059465, -4.50014046 55.31291222, -4.50202214 55.31631459, -4.50542276 55.31968523, -4.51009609 55.32393858, -4.51441426 55.32820807, -4.51834953 55.33176287, -4.52164867 55.33551663, -4.52584909 55.33870505, -4.52849713 55.3424766, -4.53244048 55.34767851, -4.53507095 55.35126714, -4.53535285 55.35164433, -4.53868897 55.3561076, -4.54165075 55.35986615, -4.54364242 55.36273418, -4.54404772 55.3654532, -4.54421522 55.3659913, -4.54511516 55.36888201, -4.54602098 55.3699382, -4.52896118 55.37263801, -4.50087758 55.35824432, -4.47661559 55.37139693, -4.44762781 55.38682775, -4.32023047 55.33238024, -4.27301552 55.31545439, -4.27763349 55.3106471)))",
    "MultiPolygon (((-4.6 55.07929898, -4.63292986 55.080, -4.62740353 55.07789383, -4.62832449 55.07617534, -4.62894805 55.07655459, -4.63067565 55.07719054, -4.63218879 55.07774751, -4.63603734 55.0783822, -4.63636727 55.07929898)))")

data <- rep(data, 1e5)

df <- tibble(Id = rep(c(1660L, 1552L, 1625L), 1e5), Coverage = data)


df$Coverage <- str_match(df$Coverage, '\\(\\(\\((.*)\\)\\)\\)')[,2]

#each element of the resulting list will be the contents of a row. 
system.time({
splits <- str_split(df$Coverage, ', ') %>%
            map(~ str_split(string = .x, pattern = ' ', simplify = TRUE)) 
})


splits_numeric <-  map(splits,~ tibble(lat = .x[, 1], long = .x[, 2],
                                       lat_4 = str_replace(lat, '^.*\\.', '') %>% str_count() < 4,
                                       long_4 = str_replace(long, '^.*\\.', '') %>% str_count() < 4))

final_df <- 
splits_numeric %>%
  map2_dfr(1:length(splits_numeric), ~ filter(.x, lat_4 | long_4) %>% mutate(row_n = .y))



final <- select(final_df[-c(3, 4)], row_n, everything())
jpdugo17
  • 6,816
  • 2
  • 11
  • 23
  • I have added the sample input to elaborate it .I need only those values that are less than 4 decimal places.I mean filter/retrieve from each 'location' or multipolygon column the lat long values that are less than 4 decimal points – user1820133 Jun 19 '21 at 23:35
  • You can use for example `dput(test3$location[1:3])` so the data is easier to copy . It will also be very helpful to know what libraries you are using in the code. – jpdugo17 Jun 20 '21 at 00:46
  • I am still waiting for a response on this one! – user1820133 Jun 26 '21 at 10:22
  • @user1820133 I'm not being able to find many numbers with less than 4 digits, but the code seems working. – jpdugo17 Jun 26 '21 at 23:55
  • Can i get the output as a dataframe with the Ids? – user1820133 Jun 27 '21 at 11:14
  • @user1820133 something like this? – jpdugo17 Jun 27 '21 at 18:38
  • did you share something? – user1820133 Jun 28 '21 at 00:14
  • @user1820133 i changed the output to a df instead of a list. – jpdugo17 Jun 28 '21 at 00:20
  • thanks thats great! but now only issue is its checking with respect to lat only, if long has less than 4 decimals its ignoring them, i mean there should be check on both either lat or long in the pair less than 4 decimals.Can you have a look? – user1820133 Jun 28 '21 at 01:16
  • @user1820133 Added the long case as well. I manually changed some values to give them less than 4 decimals. – jpdugo17 Jun 28 '21 at 02:41
  • thanks alot @jpdugo17, much appreciated! can you have a look at another query for the same input wherein the issue is rbind filling in columns automatically while i want NAs for the missing column values, here is the link https://stackoverflow.com/questions/68141372/strsplit-with-rbind-in-r-fills-missing-columns-automatically – user1820133 Jun 28 '21 at 03:25
  • @ jpdugo17 for a dataframe with over 300,000, the process is slower even than a for loop.Is there any alternative? – user1820133 Jun 28 '21 at 10:24
  • @user1820133 i edited the answer. It took approximately 20 mins to run with 300k rows. – jpdugo17 Jul 02 '21 at 04:25
  • with your original code i modified to split by ' ' instead of ',' & do check for both lat & long <4 in one go, it takes 10 min for 300K+ rows but it is the same time taken by nested for loops: i want time to be under 5 mins ideally or atleast better than for loops df$Coverage <- map(df$Coverage, ~ str_split(.x, ' ')) lat_df <- df$Coverage %>% map_depth(2, ~ str_extract(.x, '\\..*')) %>% map_depth(2, ~str_count(.x)-1) %>% map_depth(2, ~.x < 4) %>% flatten() %>% map2_df(1:length(.), ~ tibble(row_number = .y, lat_long = df$Coverage[[.y]][[1]][.x])) – user1820133 Jul 02 '21 at 04:37
0

How about something like the below code? I added a function to count decimal places from the following StackOverflow discussion: how to return number of decimal places in R

 library(tidyverse)
    library(stringr)
    library(plyr)
    
    
    decimalplaces <- function(x) {
      ifelse(abs(x - round(x)) > .Machine$double.eps^0.5,
             nchar(sub('^\\d+\\.', '', sub('0+$', '', as.character(x)))),
             0)
    }
    
    location <- 'MultiPolygon (((-4.81 55.04787153, -4.8111671 55.04787157, -4.81 55.0433575, -4.82518803 55.04184847, -4.8253126 55.04161349, -4.82757832 55.04136265, -4.82759895 55.04136035, -4.82759837 55.04134218, -4.8274984 55.03819838, -4.82748246 55.03769745, -4.82747713 55.03753027, -4.82779963 55.03692183, -4.8286453 55.03613443, -4.82866891 55.03611247, -4.82871339 55.03612473, -4.83128101 55.03683293, -4.8330557 55.0395892, -4.8375525 55.04364776, -4.84054227 55.0444988, -4.84087484 55.04485244, -4.84090187 55.04488119, -4.84095612 55.04488227, -4.84195951 55.04490218, -4.84254884 55.04506991, -4.84725796 55.04537362, -4.85193124 55.04410595, -4.85229246 55.04416239, -4.85257436 55.04393148, -4.85330305 55.04373379, -4.85701362 55.04221698, -4.85755175 55.04220086, -4.85759621 55.04219953, -4.85824979 55.04205503, -4.85838692 55.04202471, -4.86476764 55.04234263, -4.87020874 55.04127846, -4.87427377 55.04009613, -4.87963181 55.04038338, -4.88186797 55.04145598, -4.88282849 55.04439579, -4.88279397 55.04788382, -4.88311103 55.05012629, -4.89302955 55.05435568, -4.89038523 55.05775502, -4.89128426 55.05994627, -4.89638153 55.06252619, -4.91047684 55.06605598, -4.91958259 55.06674028, -4.92014849 55.06659039, -4.92766858 55.06459823, -4.93683727 55.06295485, -4.94099991 55.06176798, -4.93603545 55.0596367, -4.93175357 55.05748899, -4.92948215 55.05371823, -4.9289569 55.05058104, -4.93496102 55.0469506, -4.9448408 55.04300229, -4.94740542 55.04102839, -4.94702316 55.03882513, -4.95775252 55.03114311, -4.95654112 55.02760986, -4.95821834 55.02363228, -4.9612258 55.02153491, -4.96479791 55.02148634, -4.96918064 55.02179306, -4.97190088 55.02318987, -4.97714984 55.02070043, -4.98009117 55.01860433, -4.98104356 55.01681879, -4.98089862 55.01614925, -5.01600148 55.02320457, -4.97086238 55.0533802, -4.97679564 55.0563093, -4.9767957 55.05630926, -4.97679582 55.05630932, -4.97845681 55.05520046, -4.99005922 55.06220399, -4.97834076 55.06940482, -4.98406193 55.0724712, -4.984062 55.07247116, -4.9840621 55.07247121, -4.99546326 55.06546481, -4.99698449 55.06638257, -4.99698458 55.06638252, -4.99698469 55.06638259, -5.00265572 55.0632862, -4.98358436 55.05177683, -5.01490219 55.03084885, -5.01890859 55.03294255, -5.01890866 55.03294251, -5.01890876 55.03294256, -5.02990874 55.02599615, -5.05936233 55.03190184, -5.05931546 55.03192132, -5.05937499 55.03198274, -5.05951823 55.03193308, -5.05957754 55.03194496, -5.05969007 55.03201076, -5.05979769 55.03201246, -5.05979977 55.0320125, -5.05983004 55.03199825, -5.05992587 55.03202289, -5.05998356 55.03206189, -5.05991529 55.03209057, -5.05980628)))'
    
    
    
    location <- stringr::str_extract(location, "(?<=[(]).*?(?=\\))") %>%
      gsub('[(]',"",.)
    
    rowData <- strsplit(location, ",") %>%
      as.data.frame(stringsAsFactors = F) %>%
      set_names("LatLong") %>%
      mutate(LatLong = trimws(LatLong)) %>%
      separate(LatLong, into = c("Lat", "Long"), sep = " ", fill = "right") %>%
      mutate(Long = replace_na(Long, 0)) %>%
      mutate(Lat = as.numeric(Lat)) %>%
      mutate(Long = as.numeric(Long)) %>%
      mutate(Decimals = as.numeric(decimalplaces(abs(Lat))))%>%
      filter(Decimals < 4) %>%
      select(-Decimals)
    
    ### Some test data
    
    row1 <- t(rowData["Lat"]) %>%
      unname() %>%
      unlist() %>%
      as.data.frame()
    
    row2  <- t(sample(seq(from = -5, to = -5.99, by = -.01), size = 10, replace = TRUE)) %>%
      as.data.frame()
    
    row3 <- t(sample(seq(from = -5, to = -5.99, by = -.01), size = 15, replace = TRUE)) %>%
      as.data.frame() 
    
    ####Produce Dataframe
    
    df <- plyr::rbind.fill(row1, row2, row3) %>%
      `rownames<-`(c("Row 1", "Row 2", "Row 3"))
Blue050205
  • 276
  • 2
  • 4
  • I have added the sample input to elaborate it .I need only those values that are less than 4 decimal places.I mean filter/retrieve from each 'location' or multipolygon column the lat long values that are less than 4 decimal points – user1820133 Jun 19 '21 at 23:36
  • rowData is making multiple rows for one input row, I want to have a single out row as per the single input row like row 1 should be having all the <4 decimal values of input row 1 and so on.....can you process the new input values. – user1820133 Jun 20 '21 at 02:39
  • row1 reflects your original Location column and the output for Lat with values less than 4 decimal places. There were only two values that met the criteria: -4.81, -4.81. – Blue050205 Jun 20 '21 at 03:16
  • how to extend that to the column of multiple rows and result output should be a dataframe.I have updated the sample input. – user1820133 Jun 21 '21 at 06:57