Re: [R] apply family functions (tapply, sapply, mapply etc)

2011-07-08 Thread Uwe Ligges
On 07.07.2011 20:30, Dimitris.Kapetanakis wrote: Dear all, I am trying to use apply or similar functions in order to improve the efficiency of my code but I guess I am misunderstanding the function of these commands. What I want to do is possible to see with the following code that I use a for

[R] apply family functions (tapply, sapply, mapply etc)

2011-07-07 Thread Dimitris.Kapetanakis
Dear all, I am trying to use apply or similar functions in order to improve the efficiency of my code but I guess I am misunderstanding the function of these commands. What I want to do is possible to see with the following code that I use a for loop command. In words what I want to do are two thi

Re: [R] apply family functions

2010-08-07 Thread Jeff Newmiller
Sapply is not significantly faster than a for loop. Vectorization generally is, but you pay for it in RAM. > dat.oc <- oc[dat$Class,] > dat$Flag <- ifelse(with(dat.oc,(Open<=dat$Close_date & dat$Close_date<=Close) | (Open1<=dat$Close_date & dat$Close_date<=Close1)),"Valid","Invalid") If you

Re: [R] apply family functions

2010-08-07 Thread Wu Gong
Hi Steven, You code has two problems. One is loop structure which should be for(i in 1:length). The second is that you loop the process twice (for and sapply). Hope followed code will work. for (i in 1:length(ind)) { x <- dat$Close_date[i] dat[["Flag"]][i] <- ifelse((x >= oc[ind[[i

Re: [R] apply family functions

2010-08-06 Thread Steven Kang
ini <- as.Date("2010/1/1", "%Y/%m/%d") # Generate arbitrary data frame consisting of date values oc <- data.frame(Open = seq(ini, ini + 6, 1), Close = seq(ini + 365, ini + 365 + 6, 1), Open1 = seq(ini + 365*2, ini + 365*2 + 6, 1), Close1 = seq(ini + 365*3, ini + 365*3 + 6, 1), Open2 = seq(ini + 365

Re: [R] apply family functions

2010-08-06 Thread jim holtman
The data you provided does not match your example: > dat <- data.frame(Class = c("AAA", "C", "CC", "BB", "B", "A"), Close_date = + c(ini, ini, ini, ini+109, ini+39, ini+24), stringsAsFactors = FALSE) > dat Class Close_date 1 AAA 2010-01-01 2 C 2010-01-01 3CC 2010-01-01 4BB 2010-04-

[R] apply family functions

2010-08-05 Thread Steven Kang
Hi all, I would like to flag each record in the data according to certain conditions as specified below. For example, If "Close_date" in *dat* is between ("Open" & "Close") or ("Open1" & "Close1") or ("Open2" & "Close2") in *oc, *flag the records as "Valid", otherwise "Invalid" I would like to