Steven Kang
2009-Sep-04 04:54 UTC
[R] User defined function's argument as Subset function's input
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> xID 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> x1ID A1 A2 A3 A4 1 a 10 2 101 -60> x2ID A1 A2 A3 A4 6 a 10 2 95 3> x3ID 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]]
baptiste auguie
2009-Sep-04 09:21 UTC
[R] User defined function's argument as Subset function's input
Hi, This may come close to what you want, 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=c(-60, rep(c(0, 3), length=9))) # basic conditions cond1 <- quote(ID == "a" & A2 >1) cond2 <- quote(A1 < 10) cond3 <- quote(A1 == 10) # note the requirement to split inequalities in two conditions cond4 <- quote(A3 > 100) cond5 <- quote(A3 == 100) # compound conditions c1 <- bquote(.(cond1) & (.(cond2) | .(cond3)) & .(cond4)) c2 <- bquote(.(cond1) & !.(cond2) & !(.(cond4) | .(cond5))) c3 <- bquote(.(cond1) & !.(cond2)) subx <- function(case){ switch(case, '1' = subset(x, eval(c1)), '2' = subset(x, eval(c2)), '3' = subset(x, eval(c3))) } subx(1) subx(2) subx(3) HTH, baptiste 2009/9/4 Steven Kang <stochastickang@gmail.com>> 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. >-- _____________________________ Baptiste AuguiƩ School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK http://newton.ex.ac.uk/research/emag ______________________________ [[alternative HTML version deleted]]