Short(er) notation of selecting a part of a data.frame or other objects in R

I always get angry at my R code when I have to process dataframes, i.e. filtering out certain rows. The code gets very illegible as I tend to choose meaningful, but long, names for my objects. An example:

all.mutations.extra.large.name <- read.delim(filename)
head(all.mutations.extra.large.name)
               id        gene  pos  aa     consequence   V
  ENSG00000105732 ZN574_HUMAN   81 x/N missense_variant  3
  ENSG00000125879  OTOR_HUMAN    7 V/3 missense_variant  2
  ENSG00000129194 SOX15_HUMAN   20 N/T missense_variant  3
  ENSG00000099204 ABLM1_HUMAN   33 H/R missense_variant  2
  ENSG00000103335 PIEZ1_HUMAN   11 Q/R missense_variant  3
  ENSG00000171533  MAP6_HUMAN   39 A/G missense_variant  3

   all.mutations.extra.large.name <- all.mutations.extra.large.name[which(all.mutations.extra.large.name$gene == ZN574_HUMAN)]

So in order to kick out all other lines in which I am not interested I need to reference 3 times the object all.mutations.extra.large.name. And reating this kind of step for different columns makes the code really difficult to understand.

Therefore my question: Is there a way to filter out rows by a criterion without referencing the object 3 times. Something like this would be beautiful: myobj[,gene=="ZN574_HUMAN"]

Answers


You can use subset for that:

subset(all.mutations.extra.large.name, gene == "ZN574_HUMAN")

Several options:

all.mutations.extra.large.name <- data.frame(a=1:5, b=2:6)
within(all.mutations.extra.large.name, a[a < 3] <- 0)
  a b
1 0 2
2 0 3
3 3 4
4 4 5
5 5 6
transform(all.mutations.extra.large.name, b = b^2)
  a  b
1 1  4
2 2  9
3 3 16
4 4 25
5 5 36

Also check ?attach if you would like to avoid repetitive typing like all.mutations.extra.large.name$foo.


Need Your Help

Conditionally link a third party API in Xcode

iphone ios xcode4.2

I have a third party API that I am trying to integrate into my iOS Universal application. The API works fine if ran on a device but throws me a compile time link error when trying to run it on the

Hibernate many to many mapping a map to a list

hibernate list collections map many-to-many

I am having problems designing a many to many relation between two entities when using a Map and a List.