Re: [R] incrementation within ifelse

2013-01-09 Thread Adams, Jean
Damien,

You don't give an example of what your data frame looks like or what you
want the new column to look like (given that example data), but I created
an example data frame for z, and wrote a few lines of code to add a new
column.  Check it out and see if it comes close to doing what you want.

first - function(x) c(1, 1-(x[-1]==x[-length(x)]))
n - 25
z - data.frame(flagFoehn3_durr=sample(0:2, n, TRUE), Guetsch=sample(0:2,
n, TRUE))
z$newColumn - cumsum(first(z$flagFoehn3_durr==1)  z$flagFoehn3_durr==1)
z$newColumn[z$flagFoehn3_durr!=1] - 0

Jean




On Tue, Jan 8, 2013 at 12:33 PM, Damien Pilloud damien.pill...@gmail.comwrote:

 Dear R-helper,

 I am working on a very large data frame and I am trying to add a new column
 and write in it with certain conditions. I have try to use this code with
 the data frame p :

 ID = 0

 p[,newColumn]-
 ifelse (p$flagFoehn3_durr == 1,
 ifelse(p$Guetsch == 0,
 ID - ID ++
 ,
 ID
 )
 ,
 0
 )

 What I am trying to do is to increment the ID when p$Guetsch == 0 and to
 put this result in the column. The problem is that ID does not increment
 itself.

 An other way is to use a loop for like this example :

 ID = 0
 for (s in 1:(nrow(z))){

 z[s,newColumn]-
 if (z$flagFoehn3_durr[s] == 1){
 if(z$flagFoehn3_durr[s-1] == 0){
 ID -ID+1
 }else{
 ID
 }
 }else{
 0
 }
 }

 This work perfectly, but the problem is that it will take me more than a
 month to run it.

 Is there a way to increment with the first code I used or a way of running
 the second code faster (I have more than 1 million rows)

 Thanks!

 Cheers,

 Damien

 [[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.


[[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.


[R] incrementation within ifelse

2013-01-08 Thread Damien Pilloud
Dear R-helper,

I am working on a very large data frame and I am trying to add a new column
and write in it with certain conditions. I have try to use this code with
the data frame p :

ID = 0

p[,newColumn]-
ifelse (p$flagFoehn3_durr == 1,
ifelse(p$Guetsch == 0,
ID - ID ++
,
ID
)
,
0
)

What I am trying to do is to increment the ID when p$Guetsch == 0 and to
put this result in the column. The problem is that ID does not increment
itself.

An other way is to use a loop for like this example :

ID = 0
for (s in 1:(nrow(z))){

z[s,newColumn]-
if (z$flagFoehn3_durr[s] == 1){
if(z$flagFoehn3_durr[s-1] == 0){
ID -ID+1
}else{
ID
}
}else{
0
}
}

This work perfectly, but the problem is that it will take me more than a
month to run it.

Is there a way to increment with the first code I used or a way of running
the second code faster (I have more than 1 million rows)

Thanks!

Cheers,

Damien

[[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.