Hello, A simple question perhaps, but how do I, within each row, find the first occurence of the number 1 in the df below? I want to use this 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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [[alternative HTML version deleted]]
another approach is: 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)) years <- as.numeric(gsub("^[^0-9]+", "", names(df))) ind <- apply(sapply(df, "==", 1), 1, function (x) which(x)[1]) df$year <- years[ind] I hope it helps. Best, Dimitris On 5/4/2011 1:52 PM, Albert-Jan Roskam wrote:> Hello, > > A simple question perhaps, but how do I, within each row, find the first > occurence of the number 1 in the df below? I want to use this 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 > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > All right, but apart from the sanitation, the medicine, education, wine, public > order, irrigation, roads, a fresh water system, and public health, what have the > Romans ever done for us? > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/
You may want to look into the function 'match', which finds the first occurrence of a value. In your example, 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)) apply(df,1,match,x=1) [1] 3 2 2 2 1 NA _______________________ Patrick Breheny Assistant Professor Department of Biostatistics Department of Statistics University of Kentucky On 05/04/2011 07:52 AM, Albert-Jan Roskam wrote:> Hello, > > A simple question perhaps, but how do I, within each row, find the first > occurence of the number 1 in the df below? I want to use this 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 > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > All right, but apart from the sanitation, the medicine, education, wine, public > order, irrigation, roads, a fresh water system, and public health, what have the > Romans ever done for us? > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.