Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select string starting with numbers in R

Tags:

r

This is very simple yet I cannot get it 100% right!

I have column of data that looks like this:

"424343 Amsterdam center" 
"343423 London 42 ......"
"3434   Prague ........." 
"343345 Bratislava ...."
"! last entry ..... 25.08.2014..."
"Berlin"
...
...

I would like to replace all rows starting with letter with empty string ""

I have tried:

dataframe$column[grepl("(^[A-Z]+).*",dataframe$column)] <- ""

I'm still getting the rows like these .... "! last entry ..... 25.08.2014..."

Desired output:

 "424343 Amsterdam center" 
 "343423 London 42 ......"
 "3434   Prague ........." 
 "343345 Bratislava ...."
 ""
 ""
...
...
like image 821
Maximilian Avatar asked Sep 16 '25 22:09

Maximilian


1 Answers

You can look just for strings starting with at least one number and take all non matching results (using !), e.g.:

!grepl("^[[:digit:]]+", text)

In your example:

dataframe$column[!grepl("^[[:digit:]]+",dataframe$column)] <- ""
like image 96
sgibb Avatar answered Sep 19 '25 11:09

sgibb