Oğuzhan Akdeniz
Oğuzhan Akdeniz

Reputation: 39

R how to remove rows in a data frame based on the first character of a column

I have a big data frame and I want to remove certain rows from it based on first char of a column being a letter or a number. Sample of my data frame looks like a below:

y<-c('34TA912','JENAR','TEST','34CC515')
z<-('23.12.2015','24.12.2015','24.12.2015','25.12.2015')
abc<-data.frame(y,z)

Based on the sample above. I would like to remove second and third rows due to the value in y column in second row and third row starting with a letter instead of a number. Characters written in Y column could be anything, so only way I could filter is checking the first character without using any predefined value. If I use grep with a character, since other rows also contain letter, I could remove them aswell. Can you assist?

Upvotes: 1

Views: 7007

Answers (1)

akrun
akrun

Reputation: 887068

We can use grep. The regex ^ indicates the beginning of the string. We match numeric element ([0-9]) at the beginning of the string in the 'y' column using grep. The output will be numeric index, which we use to subset the rows of the 'abc'.

 abc[grep('^[0-9]', abc$y),]
 #        y          z
 #1 34TA912 23.12.2015
 #4 34CC515 25.12.2015

Upvotes: 5

Related Questions