Dear R users,

I have a data where I desire to subset according to certain conditions.
However, the script is very messy as there are about 30 distinct conditions.
(i.e. same script but with different conditions)

I would like to make a user defined function so that I can input the desired
conditions and just get the results accordingly.

Below is an arbitrary data set & sample statements with its outputs in
accordance with specified conditions:


x <- data.frame(ID=rep(letters[1:5],2), A1=rep(10:14,2), A2=rep(2:6,2),
A3=c(101:105,95:99), A4=3*ceil(rt(10,2)))

x1 <- subset(x, ID == "a" & A1 <= 10 & A2 > 1 & A3 > 100)  #condition 1
x2 <- subset(x, ID == "a" & A1 >= 10 & A2 > 1 & A3 < 100)  #condition 2
x3 <- subset(x, ID == "a" & A1 >= 10 & A2 > 1)                   #condition
3

> x
   ID A1 A2  A3  A4
1   a 10  2 101 -60
2   b 11  3 102   0
3   c 12  4 103   3
4   d 13  5 104   0
5   e 14  6 105   3
6   a 10  2  95   3
7   b 11  3  96   3
8   c 12  4  97   9
9   d 13  5  98   0
10  e 14  6  99   3
> x1
  ID A1 A2  A3  A4
1  a 10  2 101 -60
> x2
  ID A1 A2 A3 A4
6  a 10  2 95  3

> x3
  ID A1 A2  A3  A4
1  a 10  2 101 -60
6  a 10  2  95   3
>


I would like to make a user defined function so that I don't have to repeat
the identical statements with difference being just the conditions.

For example,

ret <- function(cond)  {
    condition <- ifelse(cond==1, ID == "a" & A1 <= 10 & A2 > 1 & A3 > 100,
ifelse(cond==2, ID == "a" & A1 >= 10 & A2 > 1 & A3 < 100, ID == "a" & A1 >=
10 & A2 > 1))
    x <- subset(x, condition)
    }

then revoke via,

ret(1) or ret(2) etc..



Greatly appreciate for enlightening me solving this problem.





Steven

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to