search for: giveyear

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

2011 May 04
2
first occurrence of a value?
...wnright 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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the...