If there are a lot more rows than columns in your data.frame then looping over columns is faster than looping over rows. Comparing f2 <- function (dataFrame) { retval <- dataFrame[, 1] for (col in dataFrame[-1]) { na <- is.na(retval) if (!any(na)) break retval[na] <- col[na] } retval } to the looping over row version f1 <- function (dataFrame) apply(dataFrame, 1, function(x) x[!is.na(x)][1])
for a 250,000 row by 3 coludata.frame, f1 takes 2.48 seconds and f1 0.02. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of Henrik Singmann > Sent: Tuesday, July 24, 2012 1:40 AM > To: r-h...@stat.math.ethz.ch > Subject: Re: [R] First value in a row > > Hi Camilo, > > as you want to work on all rows, apply() is your friend. > In the following, I use an anonymous function getting the first non-na > value while looping over each row: > > dat <- read.table(text = " > Lat Lon x1 x2 x3 > 01 10 NA NA .1 > 01 11 NA .2 .3 > 01 12 .4 .5 .6 > ", header = TRUE) > > apply(dat[,-(1:2)], 1, function(x) x[!is.na(x)][1]) > > gives: > [1] 0.1 0.2 0.4 > > Cheers, > Henrik > > Camilo Mora schrieb: > > Hi. > > > > This is likely a trivial problem but have not found a solution. Imagine > > the following dataframe: > > > > Lat Lon x1 x2 x3 > > 01 10 NA NA .1 > > 01 11 NA .2 .3 > > 01 12 .4 .5 .6 > > > > I want to generate another column that consist of the first value in > > each row from columns x1 to x3. That is > > > > NewColumn > > .1 > > .2 > > .4 > > > > Any input greatly appreciated, > > > > Thanks, > > > > Camilo > > > > > > Camilo Mora, Ph.D. > > Department of Geography, University of Hawaii > > > > ______________________________________________ > 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. ______________________________________________ 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.