[R] in which column is an entry?
Christian Schulz
ozric at web.de
Sat Jan 31 23:38:01 CET 2004
Thanks, speed is sometimes reaaly important , but
to understand in a advanced practice guide about loops.
i'm always not sure when indexing is necessary for the objects in the loop:
Different versions accept as function but didn'T run without error.
many thanks & regards,
christian
special <- function(const,modeldat,YS) {
for(i in 1:10) {
const[i]=const[i]+1 #i'know only here indexing make no sense!?
t1 <- apply(YS,1, function(x) { ifelse(all(is.na(x)) | all(na.omit(x)<0),
NA, which( x > const[i]))})
t1[is.na(t1)] <- 13
t2 <- sapply(t1,function(x) { ifelse(x ==13,0,1)})
modeldat$MONTH <- t1
modeldat$ACTIVE <- t2
modeldats <- na.omit(modeldat)
mod1<- coxph(Surv(MONTH,ACTIVE) ~ ALTER+LEISTUNG,data=modeldats)
pdf(file = "~/Survival.pdf", width = 6, height = 6, onefile = TRUE, family
= "Helvetica",title = "R Graphics Output")
plot(survfit(mod1),ylim=c(.7,1),xlab='Month',ylab='Proportion not Active') }
dev.off() }
Error in fitter(X, Y, strats, offset, init, control, weights = weights, :
NA/NaN/Inf in foreign function call (arg 6)
In addition: Warning message:
Ran out of iterations and did not converge in: fitter(X, Y, strats, offset,
init, control, weights = weights,
Am Samstag, 31. Januar 2004 18:54 schrieben Sie:
> On Sat, 31 Jan 2004, Christian Schulz wrote:
> > Yes, many thanks i have really to avoid think in loops :-)
>
> Unfortunately Chuck's solution is a loop over rows, disguised by the use
> of apply.
>
> Let us assume that the dataframe has all numeric entries and coerce to a
> matrix (as apply will, BTW).
>
> tmp <- as.matrix(df)
> tmp[is.na(tmp)] <- -1 # get rid of the NAs
> tmp <- tmp >= 0 # a logical matrix
> tmp <- cbind(tmp, TRUE) # add a fence column
>
> I am happy to loop over 43 columns, though, so
>
> for(i in 2:44) tmp[, i] <- tmp[, i] | tmp[, i-1]
> for(i in 44:2) tmp[, i] <- tmp[, i] & !tmp[, i-1]
> rtmp <- t(tmp)
> z <- row(rtmp)[rtmp]
> z[z==44] <- NA
> z
>
> is what you want. It's a lot faster (about 12x).
>
> > 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
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list