Re: [R] how to manupute data frame with conditions fill cell with previous value if next cell is zero

From: Romain Francois <rfrancois_at_mango-solutions.com>
Date: Sun, 06 Apr 2008 15:06:06 +0100

Hi,

It's more a vector question. Try this one:

> f

function( x, test = is.na(x) | x == 0 ){

   out <- x[!test][ cumsum(!test) ]
   if(test[1]) out <- c(NA,out)
   out
}
> f( c(1,0,2,1,0) )

[1] 1 1 2 1 1
> f( c(1,0,2,1,0, NA) ) # missing values are also replaced
[1] 1 1 2 1 1 1
> f( c(0,1,0,2,1,0) ) # if first is to be replaced, it is set to NA
[1] NA 1 1 2 1 1

Cheers,

Romain

-----Original Message-----
From: r-help-bounces_at_r-project.org on behalf of saikat sarkar Sent: Sun 06/04/2008 14:04
To: r-help_at_r-project.org
Subject: [R] how to manupute data frame with conditions fill cell with previous value if next cell is zero  

Dear R Experts,  

This is the 2nd time in the chat room. Its a great place to get help from R experts.
I have a data frame problem, it contains thousands of data.

part of it, I am giving for explaining the problem

 date day x y z

82  1989-04-28 Fri   2118.0   2418.80  33713         
83  1989-05-01 Mon    0.0     2414.96  33793         
84  1989-05-02 Tue 2103.1    2402.86  33955        
85  1989-05-03 Wed 2105.7   2393.70      0         
86  1989-05-04 Thu  2119.0   2384.90      0         
87  1989-05-05 Fri    2132.8   2381.96      0         


103 1989-05-29 Mon    0.0    0.00     34161         
127 1989-06-30 Fri   2151.0  2440.06 32949         
128 1989-07-03 Mon 2165.6  2452.77 33236         

129 1989-07-04 Tue 2174.4    0       35376 
130 1989-07-05 Wed 2162.9 2456.56 33310         

167 1989-08-25 Fri   2397.4  2732.36  34740         
168 1989-08-28 Mon    0.0    2743.36  34607         
169 1989-08-29 Tue  2380.8 2726.63   34688         
170 1989-08-30 Wed 2381.3  2728.15  34472         
171 1989-08-31 Thu 2387.9   2737.27  34431         
172 1989-09-01 Fri   2407.5   2752.09  34348         

173 1989-09-04 Mon 2419.2    0.00 34484         
174 1989-09-05 Tue 2426.0 2744.68 34442         
175 1989-09-06 Wed 2390.8 2719.79 34271         
176 1989-09-07 Thu 2415.9 2706.88 34153         
177 1989-09-08 Fri 2423.9 2709.54 34116         
178 1989-09-11 Mon 2400.6 2704.41 34114         
179 1989-09-12 Tue 2397.6 2707.26 34333         
180 1989-09-13 Wed 2401.5 2679.52 34287         
181 1989-09-14 Thu 2382.0 2664.89 34402        

182 1989-09-15 Fri 2366.5 2674.58     0         
183 1989-09-18 Mon 2373.8 2687.50 34473        

195 1989-10-04 Wed 2312.1 2771.09 35383         
196 1989-10-05 Thu 2281.6 2773.56 35523         
197 1989-10-06 Fri 2277.5 2785.52 35209         
198 1989-10-09 Mon 2247.0 2791.41 35376         

199 1989-10-10 Tue 2218.8 2785.33     0         
200 1989-10-11 Wed 2218.8 2773.36 35240        


In this data frame, I need to replace cells which is zero with previous value.

For example

82  1989-04-28 Fri 2118.0 2418.80 33713         
83  1989-05-01 Mon    0.0 2414.96 33793         

in 2nd line 0.0 should be replaced by 2118.0

Another Example

84  1989-05-02 Tue 2103.1 2402.86 33955        
85  1989-05-03 Wed 2105.7 2393.70     0         
86  1989-05-04 Thu 2119.0 2384.90      0         
87  1989-05-05 Fri   2132.8 2381.96      0         

3 lines filled by 33955 value.

Another example

198 1989-10-09 Mon 2247.0 2791.41 35376         
199 1989-10-10 Tue 2218.8 2785.33     0        
200 1989-10-11 Wed 2218.8 2773.36 35240   

in 2nd line filled by zero should be replaced by value 35376

Please help me.

Thanking you

saikat  

-- 
View this message in context: http://www.nabble.com/how-to-manupute-data-frame-with-conditions-fill-cell-with-previous-value-if-next-cell-is-zero-tp16524107p16524107.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help_at_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_at_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.
Received on Sun 06 Apr 2008 - 14:14:36 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 06 Apr 2008 - 14:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive