search for: j2001

Displaying 1 result from an estimated 1 matches for "j2001".

Did you mean: 2001
2011 May 04
2
first occurrence of a value?
...position to programmatically create the variable 'year'. I'v come up with a solution, but I find it downright ugly. Is there a simpler way? I was hoping for a useful built-in function that I don;t yet know about. df <- data.frame(j1999=c(0,0,0,0,1,0), j2000=c(NA, 1, 1, 1, 0, 0), j2001=c(1, 0, 1, 0, 0, 0), year=c(2001, 2000, 2000, 2000, 1999, NA)) library(gsubfn) x <- apply(df==1, 1, which) giveYear <- function(df) { return( as.numeric(gsubfn("^[^0-9]+", "", names(df)[1])) ) } df$year2 <- sapply(x, giveYear) Thanks in advance! Cheers!! Albert-Jan...