[R] Replacing null values
Niklaus Kuehnis
kuehnik_0505 at gmx-topmail.de
Sat Jul 30 23:21:45 CEST 2011
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
More information about the R-help
mailing list