Home > Enterprise >  How to remove all the letters from a string after a specific point in R?
How to remove all the letters from a string after a specific point in R?

Time:10-03

Hi I have some statistics on countries population and such here:

##   [1] "China 1439323776 0.39 5540090 153 9388211 -348399 1.7 38 61 18.47 2 "           
##   [2] "India 1380004385 0.99 13586631 464 2973190 -532687 2.2 28 35 17.70 3 "          
##   [3] "United States 331002651 0.59 1937734 36 9147420 954806 1.8 38 83 4.25 4 "       
##   [4] "Indonesia 273523615 1.07 2898047 151 1811570 -98955 2.3 30 56 3.51 5 "          
##   [5] "Pakistan 220892340 2.00 4327022 287 770880 -233379 3.6 23 35 2.83 6 "           
##   [6] "Brazil 212559417 0.72 1509890 25 8358140 21200 1.7 33 88 2.73 7 "               
##   [7] "Nigeria 206139589 2.58 5175990 226 910770 -60000 5.4 18 52 2.64 8 "             
##   [8] "Bangladesh 164689383 1.01 1643222 1265 130170 -369501 2.1 28 39 2.11 9 "        
##   [9] "Russia 145934462 0.04 62206 9 16376870 182456 1.8 40 74 1.87 10 "               
##  [10] "Mexico 128932753 1.06 1357224 66 1943950 -60000 2.1 29 84 1.65 11 "             
##  [11] "Japan 126476461 -0.30 -383840 347 364555 71560 1.4 48 92 1.62 12 " 

However the last one looks like this:

## [235] "Holy See 801 0.25 2 2003 0  N.A. N.A. N.A. 0.00 </tr> </tbody></table><br><div class=\"source-tableSource: <strong>Worldometer</strong> (<a href=\"/www.Worldometers.info</a>) <div class=\"source-table-elabElaboration of data by United Nations Department of Economic and Social Affairs Population Division. <a href=\"https://esa.un.org/unpd/wpp/World Population Prospects: The 2019 Revision</a>. (Medium-fertility variant).</div></div></div> <div align=\"center\" id=\"worldometers_728x90_970x90_BTF<script data-cfasync=\"false\" type=\"text/javascript freestar.config.enabled_slots.push({ placementName: \"worldometers_728x90_970x90_BTF\" slotId: \"worldometers_728x90_970x90_BTF\" });</script></div></div></div></div><script async src=\"https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js</script><ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"ca-pub-3701697624350410\" data-ad-slot=\"6995709761\" data-ad-format=\"auto\" data-full-width-responsive=\"true</ins><script> (adsbygoogle = window.adsbygoogle || []).push({});</script><div style=\"margin-top:40px</div><script async src=\"https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js</script><ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"ca-pub-3701697624350410\" data-ad-slot=\"1743383089\" data-ad-format=\"auto\" data-full-width-responsive=\"true</ins><script> (adsbygoogle = window.adsbygoogle || []).push({});</script></div> <footer><div class=\"footerlinks<div style=\"margin-bottom:20px<a href=\"/<img src=\"/img/worldometers-logo-footer.png\" border=\"0\" class=\"img-footer</a></div><a href=\"/about/about</a> | <a href=\"/faq/faq</a> | <a href=\"/languages/languages</a> | <a href=\"/contact/contact</a></div><ul class=\"list-inline text-center socialbuttons<li><a href=\"/newsletter-subscribe/\" data-toggle=\"tooltip\" data-placement=\"bottom\" title=\"Newsletter<i class=\"fa fa-bullhorn fa-round</i></a></li> <li><a href=\"https://twitter.com/Worldometers<i class=\"fa fa-twitter fa-round</i></a></li> <li><a href=\"https://www.facebook.com/Worldometers.info<i class=\"fa fa-facebook fa-round</i></a></li></ul> <div class=\"copy&copy; Copyright Worldometers.info - All rights reserved - <a href=\"/disclaimer/Disclaimer & Privacy Policy</a></div></footer><script type=\"text/javascript\" src=\"//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-54615fa823b2af68\" async=\"async</script> <script type=\"text/javascript (function() { function async_load(script_url){ var protocol = ('https:' == document.location.protocol ? 'https://' : 'http://'); var s = document.createElement('script'); s.src = protocol   script_url; var x = document.getElementsByTagName('script')[0]; x.parentNode.insertBefore(s x); } bm_website_code = '7849CD9451D34931'; jQuery(document).ready(function(){async_load('asset.pagefair.com/measure.min.js')}); jQuery(document).ready(function(){async_load('asset.pagefair.net/ads.min.js')}); })();</script> <script type=\"text/javascriptvar _qevents = _qevents || "

I want to remove all the letters and stuff after the 0.00. How do I do this? TIA!

CodePudding user response:

We can use trimws from base R

trimws(str1, whitespace  = "\\s \\</.*")
[1] "Holy See 801 0.25 2 2003 0  N.A. N.A. N.A. 0.00"

data

str1 <- "Holy See 801 0.25 2 2003 0  N.A. N.A. N.A. 0.00 </tr> </tbody></table><br><div class=\"source-tableSource: <strong>Worldometer</strong> (<a href=\"/www.Worldometers.info</a>) <div class=\"source-table-elabElaboration of data by United Nations Department of Economic and Social Affairs Population Division. <a href=\"https://esa.un.org/unpd/wpp/World Population Prospects: The 2019 Revision</a>. (Medium-fertility variant).</div></div></div> <div align=\"center\" id=\"worldometers_728x90_970x90_BTF<script data-cfasync=\"false\" type=\"text/javascript freestar.config.enabled_slots.push({ placementName: \"worldometers_728x90_970x90_BTF\" slotId: \"worldometers_728x90_970x90_BTF\" });</script></div></div></div></div><script async src=\"https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js</script><ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"ca-pub-3701697624350410\" data-ad-slot=\"6995709761\" data-ad-format=\"auto\" data-full-width-responsive=\"true</ins><script> (adsbygoogle = window.adsbygoogle || []).push({});</script><div style=\"margin-top:40px</div><script async src=\"https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js</script><ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"ca-pub-3701697624350410\" data-ad-slot=\"1743383089\" data-ad-format=\"auto\" data-full-width-responsive=\"true</ins><script> (adsbygoogle = window.adsbygoogle || []).push({});</script></div> <footer><div class=\"footerlinks<div style=\"margin-bottom:20px<a href=\"/<img src=\"/img/worldometers-logo-footer.png\" border=\"0\" class=\"img-footer</a></div><a href=\"/about/about</a> | <a href=\"/faq/faq</a> | <a href=\"/languages/languages</a> | <a href=\"/contact/contact</a></div><ul class=\"list-inline text-center socialbuttons<li><a href=\"/newsletter-subscribe/\" data-toggle=\"tooltip\" data-placement=\"bottom\" title=\"Newsletter<i class=\"fa fa-bullhorn fa-round</i></a></li> <li><a href=\"https://twitter.com/Worldometers<i class=\"fa fa-twitter fa-round</i></a></li> <li><a href=\"https://www.facebook.com/Worldometers.info<i class=\"fa fa-facebook fa-round</i></a></li></ul> <div class=\"copy&copy; Copyright Worldometers.info - All rights reserved - <a href=\"/disclaimer/Disclaimer & Privacy Policy</a></div></footer><script type=\"text/javascript\" src=\"//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-54615fa823b2af68\" async=\"async</script> <script type=\"text/javascript (function() { function async_load(script_url){ var protocol = ('https:' == document.location.protocol ? 'https://' : 'http://'); var s = document.createElement('script'); s.src = protocol   script_url; var x = document.getElementsByTagName('script')[0]; x.parentNode.insertBefore(s x); } bm_website_code = '7849CD9451D34931'; jQuery(document).ready(function(){async_load('asset.pagefair.com/measure.min.js')}); jQuery(document).ready(function(){async_load('asset.pagefair.net/ads.min.js')}); })();</script> <script type=\"text/javascriptvar _qevents = _qevents || "
  • Related