I would like to reorder a two-column table by column A, then fill column B with the values above it. For example: Original:A B2 545 NA8 784 NULL3 269 NULL First sort by column A:A B2 543 264 NULL5 NA8 789 NULL Then replace null/na values in column B with the value above it:A B2 543 264 265 268 789 78 What is the best way to do this?Jeff [[alternative HTML version deleted]]
R. Michael Weylandt <michael.weylandt@gmail.com>
2011-Jul-30 15:24 UTC
[R] Replacing null values
This doesn't sound like it would be too hard to do at all, but I really can't read your table: could you resend the table and the desired intermediate and final steps but format them more legibly? Something like: A B 2 545 NA8 784 NULL 3 269 NULL assuming that is where you are starting. I'm not entirely sure what you expect to happen moving from one table to another, because the numbers change which seems that you are hoping for more than a short and fill missing values....(545 becomes 543? 264 becomes 265 and 268?) Again, it shouldn't be hard, just provide a more legible request and you'll be on your way. Michael Weylandt On Sat, Jul 30, 2011 at 1:25 AM, Jeffrey Joh <johjeffrey@hotmail.com> wrote:> > > > > I would like to reorder a two-column table by column A, then fill column B > with the values above it. For example: Original:A B2 545 NA8 784 > NULL3 269 NULL First sort by column A:A B2 543 264 NULL5 > NA8 789 NULL Then replace null/na values in column B with the > value above it:A B2 543 264 265 268 789 > 78 What is the best way to do this?Jeff > [[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]]
On Jul 30, 2011, at 2:25 AM, Jeffrey Joh wrote:> > I would like to reorder a two-column table by column A, then fill > column B with the values above it. For example: Original:A B2 > 545 NA8 784 NULL3 269 NULL First sort by column A:A > B2 543 264 NULL5 NA8 789 NULL Then replace > null/na values in column B with the value above it:A > B2 543 264 265 268 789 78 What is the > best way to do this?Jeff > [[alternative HTML version deleted]]The "best way to do this" meaning to get help on _this_ mailing list, is to post in plain text and to post the results of dput() on this "two-column table". All this is explained here:> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- David Winsemius, MD West Hartford, CT
Sorry about the last message. I forgot to turn the HTML off. I would like to reorder a table by column A, then fill column B with the values above it.? For example: A?? B 2?? 54 5???NA 8?? 78 4?? NULL 3?? 26 9?? NULL ? First sort by column A: A?? B 2?? 54 3?? 26 4?? NULL 5?? NA 8?? 78 9?? NULL ? Then replace null/na values in column B with the value above it: A?? B 2?? 54 3?? 26 4?? 26 5?? 26 8?? 78 9?? 78 Jeff> CC: r-help at r-project.org > From: dwinsemius at comcast.net > To: johjeffrey at hotmail.com > Subject: Re: [R] Replacing null values > Date: Sat, 30 Jul 2011 11:45:21 -0400 > > > On Jul 30, 2011, at 2:25 AM, Jeffrey Joh wrote: > > > > I would like to reorder a two-column table by column A, then fill > > column B with the values above it. For example: Original:A B2 > > 545 NA8 784 NULL3 269 NULL First sort by column A:A > > B2 543 264 NULL5 NA8 789 NULL Then replace > > null/na values in column B with the value above it:A > > B2 543 264 265 268 789 78 What is the > > best way to do this?Jeff > > [[alternative HTML version deleted]] > > The "best way to do this" meaning to get help on _this_ mailing list, > is to post in plain text and to post the results of dput() on this > "two-column table". > > All this is explained here: > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > -- > > David Winsemius, MD > West Hartford, CT >
On 30.07.2011 21:58, Jeffrey Joh wrote:> Sorry about the last message. I forgot to turn the HTML off. > I would like to reorder a table by column A,df2 <- df[order(df$A),]> then fill column B with the values above it.for (i in 1:nrow(df2)) { if (is.na(df2$B[i]) | df2$B[i] == "NULL") df2$B[i] <- df2$B[i-1] } Assumption: <NA> denotes missing value, not a character string "NA". HTH, Nik> For example: > A B > 2 54 > 5 NA > 8 78 > 4 NULL > 3 26 > 9 NULL > > First sort by column A: > A B > 2 54 > 3 26 > 4 NULL > 5 NA > 8 78 > 9 NULL > > Then replace null/na values in column B with the value above it: > A B > 2 54 > 3 26 > 4 26 > 5 26 > 8 78 > 9 78 > > Jeff