[R] Replacing null values

jim holtman jholtman at gmail.com
Sat Jul 30 22:11:06 CEST 2011


I am assuming that what you say is NULL is really a character string
"NULL", so try this:

> x
  A    B
1 2   54
2 5 <NA>
3 8   78
4 4 NULL
5 3   26
6 9 NULL
> str(x)
'data.frame':   6 obs. of  2 variables:
 $ A: int  2 5 8 4 3 9
 $ B: Factor w/ 4 levels "26","54","78",..: 2 NA 3 4 1 4
> dput(x)
structure(list(A = c(2L, 5L, 8L, 4L, 3L, 9L), B = structure(c(2L,
NA, 3L, 4L, 1L, 4L), .Label = c("26", "54", "78", "NULL"), class =
"factor")), .Names = c("A",
"B"), class = "data.frame", row.names = c(NA, -6L))
> x <- x[order(x$A),]
> x
  A    B
1 2   54
5 3   26
4 4 NULL
2 5 <NA>
3 8   78
6 9 NULL
> x$B[x$B == "NULL"] <- NA
> x
  A    B
1 2   54
5 3   26
4 4 <NA>
2 5 <NA>
3 8   78
6 9 <NA>
> require(zoo)
Loading required package: zoo
> x$newB <- na.locf(x$B)
> x
  A    B newB
1 2   54   54
5 3   26   26
4 4 <NA>   26
2 5 <NA>   26
3 8   78   78
6 9 <NA>   78
>


On Sat, Jul 30, 2011 at 3:58 PM, Jeffrey Joh <johjeffrey at hotmail.com> wrote:
>
> 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
>>
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list