[R] Recoding several variables into one use the most recent data

2011-06-27 Thread Christopher Desjardins
Hi,
I have the following data management issue. I am trying to combine multiple
years of ethnicity data into one variable called ethnic. The data looks
similar to the following

idethnic07ethnic08   ethnic09ethnic10
1  1  1 11
2  1  1 22
3  3  4 4NA
4  2  3   NANA

So, what I'd like to do is create a variable called 'ethnic' and I'd like to
have this variable be filled with the most recent data available. So
ethnic10 would have the highest priority, then ethnic09, followed by
ethnic08, and finally ethnic07.  So the ethnic variable based on the data
above would look like the following:

ethnic
   1
   2
   4
   3

I thought an ifelse() statement might work but I seem to be writing over my
data every time I do this.

Thanks,
Chris

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Recoding several variables into one use the most recent data

2011-06-27 Thread David Winsemius


On Jun 27, 2011, at 12:56 PM, Christopher Desjardins wrote:


Hi,
I have the following data management issue. I am trying to combine  
multiple
years of ethnicity data into one variable called ethnic. The data  
looks

similar to the following

idethnic07ethnic08   ethnic09ethnic10
1  1  1 11
2  1  1 22
3  3  4 4NA
4  2  3   NANA

So, what I'd like to do is create a variable called 'ethnic' and I'd  
like to

have this variable be filled with the most recent data available. So
ethnic10 would have the highest priority, then ethnic09, followed by
ethnic08, and finally ethnic07.  So the ethnic variable based on the  
data

above would look like the following:

ethnic
  1
  2
  4
  3


rd.txt - function(txt, header=TRUE, ...) {
 rd - read.table(textConnection(txt), header=header, ...)
   closeAllConnections()
 rd }

 tail_noNA - function(x,n) tail(x[!is.na(x)], n)
 tail_noNA(c(1,2,3,NA),1)
[1] 3
 dat -rd.txt(idethnic07ethnic08   ethnic09ethnic10
+ 1  1  1 11
+ 2  1  1 22
+ 3  3  4 4NA
+ 4  2  3   NANA)
 apply(dat, 1, tail_noNA, 1)
[1] 1 2 4 3

 as.matrix(apply(dat, 1, tail_noNA, 1))
 [,1]
[1,]1
[2,]2
[3,]4
[4,]3


I thought an ifelse() statement might work but I seem to be writing  
over my

data every time I do this.

Thanks,
Chris

[[alternative HTML version deleted]]

__
R-help@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.


David Winsemius, MD
West Hartford, CT

__
R-help@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.