[R] Filling in missing values in a column based on previousandfollowingvalues

John McKown john.archie.mckown at gmail.com
Mon Aug 4 16:36:04 CEST 2014


On Mon, Aug 4, 2014 at 9:01 AM, Florian Denzinger
<florian.denzinger at uzh.ch> wrote:
> Hi John,
>
> I only meant the subsetting of the dataframe by using the zoo function suggested by Gerrit! Not a new solution/function, I am afraid.
> Regards,
> F

I take it that means you _don't_ have a solution. If not, or even if
you do, my solution is below. There likely is a better way to do it.
I'm still learning R and there is a _LOT_ of things in all those
packages which I don't know.

library(reshape2); # needed for dcast()
#
# create variable
florian <- data.frame(
NAME=c('Sample1','Sample1','Sample1','Sample1','Sample1','Sample1','Sample1',
'Sample1','Sample1','Sample1','Sample1','Sample2','Sample2','Sample3','Sample3',
'Sample3','Sample3'),
YEAR=c( 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 1964,
1965, 2002, 2003, 2004, 2005),
ID=c( 354 , 354 ,NA ,NA ,NA ,NA ,NA , 354 , 354 , 354 , 354 , 1342 ,
1342 , 859 ,NA ,NA , 859 ),
VALUE=c( 45 , 23 , 66 , 98 , 36 , 59 , 64 , 23 , 69 , 94 , 24 , 7 , 24 , 90 ,
93 , 53 , 98 ),
CUMMB=c( 45 , 68 , 134 , 232 , 268 , 327 , 391 , 414 , 483 , 577 , 601 , 7 , 31
, 90 , 183 , 236 , 334 ));
#
# start of solution: y is a temporary table I need to
# find the non-NA ID values per NAME value.
# I actually use max() but ASSuME that all non-NA value are ==
# !is.na() is used to remove NA values from consideration.
y <- dcast(florian[!is.na(florian$ID),],NAME ~
.,value.var="ID",fun.aggregate=max,fill=-1,drop=FALSE);
names(y) <- c("NAME","IDy"); # Nicer names that dcast() makes.
#
# The real work is done in merge()
florian <- merge(x=florian,y=y,by=c("NAME"),all.x=TRUE);
florian$ID <- florian$IDy; #copy merged values to ID column
florian$IDy <- NULL; # remove temporary column
rm(y); # erase temporary frame.

-- 
There is nothing more pleasant than traveling and meeting new people!
Genghis Khan

Maranatha! <><
John McKown



More information about the R-help mailing list