[R] Filling in missing values in a column based on previous and following values

fd florian.denzinger at uzh.ch
Mon Aug 4 11:02:07 CEST 2014


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.



More information about the R-help mailing list