biotinker
biotinker

Reputation: 51

Is there any sort of 'default' variable in R, like in Perl?

So, I want to select some values out of a vector based on their output from a certain function.

Where, for example:

RES <- c("CTSD_1", "CTSD_2", "ARID3A")
RE <- RES[1]
strhead(RE, -2)

Would return "CTSD"

Now, what I want to do is something along these lines:

RES[strhead($_, -2) == strhead(RE, -2)]

Where $_ would be replaced with whatever the respective value of RES is. Is there a good way to do this?

I'm aware I could just write a for loop and do it like that, but it would be nice to have it in one line.

Upvotes: 0

Views: 42

Answers (1)

Oliver Keyes
Oliver Keyes

Reputation: 3294

So, you just want to get "those elements in RES where the first two characters match [the first two characters of the first entry in RES]"? If so, sort of; R is a vectorised language. When you perform an operation on a vector, like subsetting, what you're actually doing is performing that test on each /element/ of the vector. So you want something like:

RES <- RES[strhead(RES, -2) == strhead(RES[1],-2)]

Upvotes: 2

Related Questions