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

2008-04-06 Thread saikat sarkar

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 yz

82  1989-04-28 Fri   2118.0   2418.80  33713 
83  1989-05-01 Mon0.0 2414.96  33793 
84  1989-05-02 Tue 2103.12402.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 Fri2132.8   2381.96  0 


103 1989-05-29 Mon0.00.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.40   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 Mon0.02743.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.20.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 Mon0.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@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.


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

2008-04-06 Thread Romain Francois

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: [EMAIL PROTECTED] on behalf of saikat sarkar
Sent: Sun 06/04/2008 14:04
To: r-help@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 yz

82  1989-04-28 Fri   2118.0   2418.80  33713 
83  1989-05-01 Mon0.0 2414.96  33793 
84  1989-05-02 Tue 2103.12402.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 Fri2132.8   2381.96  0 


103 1989-05-29 Mon0.00.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.40   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 Mon0.02743.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.20.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 Mon0.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@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.


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

2008-04-06 Thread Gabor Grothendieck
On Sun, Apr 6, 2008 at 9:04 AM, saikat sarkar <[EMAIL PROTECTED]> wrote:

> This is the 2nd time in the chat room. Its a great place to get help from R
> experts.

I assume you are referring to this email list.  I am not aware of
a chat room but if it exists that is something different from this
list.

> I have a data frame problem, it contains thousands of data.

Actually you don't have a data frame problem.  You have a time
series problem and by representing it as a data frame instead of
a time series you are running into one problem after another that
would be trivially addressed with the correct representation.

See ?na.locf in the zoo package.

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


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

2008-04-06 Thread Bill.Venables
There may well be neater ways to do this, but if you have only a 
limited number of zeros in any run, this is probably as quick as 
any.  Suppose your data frame is 'dat':

> fixCol <- function(x) {
y <- x
n <- length(x)
while(any(zx <- x == 0)) {  
y <- c(NA, y[-n])
x[zx] <- y[zx]
}
x
  }
> dat <- transform(dat, x = fixCol(x), 
y = fixCol(y), z = fixCol(z))

If you have a great number of columns to fix up like this, a more 
succinct way would be something like

> dat[, 3:5] <- lapply(dat[, 3:5], fixCol) 



Bill Venables
CSIRO Laboratories
PO Box 120, Cleveland, 4163
AUSTRALIA
Office Phone (email preferred): +61 7 3826 7251
Fax (if absolutely necessary):  +61 7 3826 7304
Mobile: +61 4 8819 4402
Home Phone: +61 7 3286 7700
mailto:[EMAIL PROTECTED]
http://www.cmis.csiro.au/bill.venables/ 

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
On Behalf Of saikat sarkar
Sent: Sunday, 6 April 2008 11:05 PM
To: r-help@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 yz

82  1989-04-28 Fri   2118.0   2418.80  33713 
83  1989-05-01 Mon0.0 2414.96  33793 
84  1989-05-02 Tue 2103.12402.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 Fri2132.8   2381.96  0 


103 1989-05-29 Mon0.00.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.40   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 Mon0.02743.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.20.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 Mon0.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-ce
ll-with-previous-value-if-next-cell-is-zero-tp16524107p16524107.html
Sent from the R help mailing list archive at Nabble.com.

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