Hi, df is a data.frame with 43 colums and 29877 rows with lot of NA. I want the column number for all respondendts in one column where is the first entry >=0 as columnnumber. my first step: time <- function(df) + { for (i in 1:length(df [,1])) { + which(df[i,1]:df[i,43] >= 0) + } + }> > t1 <- time(YS)Error in df[i, 1]:df[i, 43] : NA/NaN argument Many thanks for help , regards christian
Christian Schulz wrote:> df is a data.frame with 43 colums and 29877 rows with lot of NA. > I want the column number for all respondendts in one column > where is the first entry >=0 as columnnumber. > > my first step: > time <- function(df) > + { for (i in 1:length(df [,1])) { > + which(df[i,1]:df[i,43] >= 0) > + } > + } > > >>t1 <- time(YS) > > Error in df[i, 1]:df[i, 43] : NA/NaN argumentI am not sure, but I think you might want something like this: t1 <- apply(df, 1, function(x){ ifelse(all(is.na(x)) | all(na.omit(x) < 0), NA, which(x >= 0))}) hope this helps, Chuck Cleland -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894
Yes, many thanks i have really to avoid think in loops :-) christian Am Samstag, 31. Januar 2004 16:57 schrieb Chuck Cleland:> Christian Schulz wrote: > > df is a data.frame with 43 colums and 29877 rows with lot of NA. > > I want the column number for all respondendts in one column > > where is the first entry >=0 as columnnumber. > > > > my first step: > > time <- function(df) > > + { for (i in 1:length(df [,1])) { > > + which(df[i,1]:df[i,43] >= 0) > > + } > > + } > > > >>t1 <- time(YS) > > > > Error in df[i, 1]:df[i, 43] : NA/NaN argument > > I am not sure, but I think you might want something like this: > > t1 <- apply(df, 1, function(x){ > ifelse(all(is.na(x)) | all(na.omit(x) < 0), > NA, which(x >= 0))}) > > hope this helps, > > Chuck Cleland