saikat sarkar
2008-Apr-06 13:04 UTC
[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.
Romain Francois
2008-Apr-06 14:06 UTC
[R] how to manupute data frame with conditions fill cell with previous value if next cell is zero
Un texte encapsul? et encod? dans un jeu de caract?res inconnu a ?t? nettoy?... Nom : non disponible Url : https://stat.ethz.ch/pipermail/r-help/attachments/20080406/cb803645/attachment.pl
Gabor Grothendieck
2008-Apr-06 16:08 UTC
[R] how to manupute data frame with conditions fill cell with previous value if next cell is zero
On Sun, Apr 6, 2008 at 9:04 AM, saikat sarkar <dipa_sanyal at yahoo.com> 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.
Bill.Venables at csiro.au
2008-Apr-06 23:30 UTC
[R] how to manupute data frame with conditions fill cell with previous value if next cell is zero
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:Bill.Venables at csiro.au http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of saikat sarkar Sent: Sunday, 6 April 2008 11:05 PM 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-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 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.