Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

The following is my customized data frame:

data.1 <-read.csv(text = "

crop,treat,rep,x_trait,y_trait

species_A,control,1,12.2,1298.9

species_A,control,1,59.3,6845.9

species_A,control,1,227.1,13973.2

species_A,control,2,22.7,2773.8

species_A,control,2,160.3,21957.5

species_A,control,2,214.1,27503.1

species_A,stress,1,19.1,2277.9

species_A,stress,1,68.2,4582.6

species_A,stress,1,107.7,12202.9

species_A,stress,2,15.5,1801.0

species_A,stress,2,69.5,4089.6

species_A,stress,2,96.8,5490.4

species_B,control,1,6.5,1284.8

species_B,control,1,74.7,15478.6

species_B,control,1,164.0,42428.6

species_B,control,2,17.8,3431.4

species_B,control,2,19.8,2441.2

species_B,control,2,78.0,15360.5

species_B,stress,1,12.4,2180.7

species_B,stress,1,53.9,10656.2

species_B,stress,1,56.5,6414.4

species_B,stress,2,11.5,2188.6

species_B,stress,2,30.5,4511.1

species_B,stress,2,45.9,9276.5

")

I just want to filter y_trait >1000 but note that only for species_A that are under the control treatment. The following is the code that I have implemented, but unable to do so:

data.2 <- data.1 %>% filter(if_else(crop == "species_A" | treat == "control", 

                                    filter(y_trait>1000)))

Any hint would be appreciated.

1 Answer

0 votes
by (108k points)

While observing your code, what you can do is just combine the conditions with &

library(dplyr)

data.1 %>% filter(crop == "species_A" & treat == "control" & y_trait > 1000)

Or if you don't want to use any package then in R programming, you can do that with the following code:

subset(data.1, crop == "species_A" & treat == "control" & y_trait > 1000)

One more thing I want to add here, the code that you have shared has lot of whitespaces, try to remove them using trimws.

data.1$crop <- trimws(data.1$crop)

Browse Categories

...