Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find occurrence of string starting with a value in R

Tags:

r

Is there a function for printing the total number of values contained in the dataset beginning with (a value)?

consider this dataset of 4 version numbers,

df <- c("1.20", "3.1.20", "2.45", "1.10", "1.67.4.3", "5.200.1", "70.1.2.7")

I need to only print version numbers 1.x. My output would be:

1.20, 1.10, 1.67.4.3

(becasue these are version numbers starting with "1." I do not want to print 3.1.20 or 70.1.2.7 becasue they do not start with "1." eventhough they contain "1." as a substring

like image 791
vardha Avatar asked Dec 04 '25 14:12

vardha


1 Answers

df <- c("1.20", "3.1.20", "2.45", "1.10", "1.67.4.3", "5.200.1", "70.1.2.7")
grep("^1\\.", df, value = TRUE)
like image 73
Andrelrms Avatar answered Dec 07 '25 06:12

Andrelrms



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!