Jane Dow Jane Dow - 18 days ago 8
R Question

Subset function in R is not subsetting the way I want it to

I am trying to subset my dataset on the basis of species RPKM value and dnds value, such that for each specie the RPKM value can range from 0 to 170, and dnds value can range from 0.10 to 0.40. I've used the subset functions as shown below. At first I tried adding these dimensions in one subset function but it wasn't working. Then I broke it down (as shown below). It seems to work for RPKM but I am still getting dNdS values greater than 0.40.
Could anyone tell me what I am doing wrong here.

This is my code:

subset_data <- subset(mammals, mammals$RPKM <= 170)

subset_data2 <- subset(subset_data,mammals$RPKM >= 0)

subset_data3 <- subset(subset_data2,mammals$dNdS >= 0.10)

subset_data4 <- subset(subset_data3,mammals$dNdS <= 0.40)

Answer

1) Run

str(mammals)

example

> str(iris)
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

Check the variable RPKM & dNdS types. They should be "num" or "int".

2) Use ?base::Logic

example

> subset(iris, Sepal.Length>6&Sepal.Width<3&Petal.Length<5&Petal.Width<1.4)
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
59          6.6         2.9          4.6         1.3 versicolor
72          6.1         2.8          4.0         1.3 versicolor
74          6.1         2.8          4.7         1.2 versicolor
75          6.4         2.9          4.3         1.3 versicolor
88          6.3         2.3          4.4         1.3 versicolor
98          6.2         2.9          4.3         1.3 versicolor

In your case

subset(mammals, mammals$RPKM <= 170 & mammals$RPKM >= 0 & mammals$dNdS >= 0.1 & mammals$dNdS <= 0dNdS.4)