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

Florian Denzinger florian.denzinger at uzh.ch
Mon Aug 4 11:39:41 CEST 2014


this is great, thanks! 

one problem I noticed though is that it fills all NA values in every column, is it possible to specify only one column, e.g. only ID (I have NA values in another column I want to keep)

Kind regards,
Florian

 

Am 04.08.2014 um 11:31 schrieb Gerrit Eichner <Gerrit.Eichner at math.uni-giessen.de>:

> Hello, Florian,
> 
> function na.locf() from package zoo mightdo what you want.
> 
> Hth --  Gerrit
> 
> ---------------------------------------------------------------------
> Dr. Gerrit Eichner                   Mathematical Institute, Room 212
> gerrit.eichner at math.uni-giessen.de   Justus-Liebig-University Giessen
> Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
> Fax: +49-(0)641-99-32109        http://www.uni-giessen.de/cms/eichner
> ---------------------------------------------------------------------
> 
> On Mon, 4 Aug 2014, fd wrote:
> 
>> I have the following .csv file containing about 40000 values (here only an
>> extract and simplified version):
>> 
>> NAME  ;	YEAR; ID;	VALUE; CUMMB
>> Sample1;	1998;	 354;	45;	   45
>> Sample1;	1999;	 354;	23;	   68
>> Sample1;	2000;	 NA;	66;	   134
>> Sample1;	2001;	 NA;	98;	   232
>> Sample1;	2002;	 NA;	36;	   268
>> Sample1;	2003;	 NA;	59;	   327
>> Sample1;	2004;	 NA;	64;	   391
>> Sample1;	2005;	 354;	23;	   414
>> Sample1;	2006;	 354;	69;	   483
>> Sample1;	2007;	 354;	94;	   577
>> Sample1;	2008;	 354;	24;	   601
>> Sample2;	1964;	1342;	7;	      7
>> Sample2;	1965;	1342;	24;	     31
>> Sample3;	2002;	 859;	90;	     90
>> Sample3;	2003;	  NA;	93;	    183
>> Sample3;	2004;	  NA;	53;	    236
>> Sample3;	2005;	 859;	98;	    334
>> 
>> What I would like to do is to replace the NA values in ID with the values
>> from the ID. E.g. all values in ID from Sample 1 should have the value 354;
>> all values in ID from Sample 3 should have the value 859 etc.
>> 
>> Is there a simple way to do this?
>> 
>> Thanks for your help.
>> 
>> 
>> 
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Filling-in-missing-values-in-a-column-based-on-previous-and-following-values-tp4694993.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.



More information about the R-help mailing list