grep-like function in R

859 Views Asked by At

I'm trying to write a program in R which would take in a .pdb file and give out a .xyz-file.

I'm having problems with erasing some rows that contain useless data. There are around 30-40 thousand rows, from which I would only need about 3000. The rows that contain the useful information start with the word "ATOM".

In unix terminal I would just use the command

grep ATOM < filename.pdb > newfile.xyz

but I have no idea how to achieve the same result with R.

Thank you for your help!

1

There are 1 best solutions below

0
On

You should be able to use grep, and depending on your specific situation, perhaps substr.

For example

#Random string variable
stringVar <- c("abcdefg", "defg", "eff", "abc")

#find the location of variables starting with "abc"
abcLoc <- grep("abc", substr(stringVar, 1, 3))

#Extract "abc" instances
out <- stringVar[abcLoc]
out

Note that the substr part limits the search to only the first three characters of each element of stringVar (e.g., "abc", "def", etc.). This may not be strictly necessary but I've found it to be very useful at times. For example, if you had an element like "defabc" that you didn't want to include, using substr would ensure it wouldn't be "found" by grep.

Hope it's helpful.