Melanie Ann Harsch
2007-Jun-06 22:36 UTC
[R] error message: only first element in each line of matrix used
I have a matrix and am trying to write a code to 1. identify all neg values along each line 2. for each neg value I need to identify min(j+3) 3. end with this code: eq[i,j]<- ifelse(mat.r[i,j] < (0.5*mat.s[i,j]), mat.all[i,j], 0) This is the code I have so far. I have tried several different methods but I keep getting the same error message that the condition has length >1 and only the first element will be used. Any suggestions? int <- 3 for(i in 1:nrow(mat.all)) { start.year <- min(which(is.na(mat.all[i,])==F & (mat.all[i,]<0))) fin.year <- max(which(is.na(mat.all[i,])==F)) for(j in start.year:fin.year) { if(mat.all[i,(j-int):(j-1)]<0){ mat.s[i,j] <- ifelse((mat.all[i,(j-int):(j-1)])==pmin, mat.all[i,j], 0); mat.r [i,j] <- which.max((mat.all[i,(j+1):(j+int)]) > 0) } }}
jim holtman
2007-Jun-07 00:56 UTC
[R] error message: only first element in each line of matrix used
your problem is in the statement: if(mat.all[i,(j-int):(j-1)]<0){ you have multiple values in the comparison and the 'if' statement can only use a single value. Do you want the statement to read: if(all(mat.all[i,(j-int):(j-1)]<0)){ which will test for 'all' the comparisons to be true? On 6/6/07, Melanie Ann Harsch <harschm@lincoln.ac.nz> wrote:> > I have a matrix and am trying to write a code to > 1. identify all neg values along each line > 2. for each neg value I need to identify min(j+3) > 3. end with this code: eq[i,j]<- ifelse(mat.r[i,j] < (0.5*mat.s[i,j]), > mat.all[i,j], 0) > > This is the code I have so far. I have tried several different methods > but I > keep getting the same error message that the condition has length >1 and > only the first element will be used. > Any suggestions? > > int <- 3 > for(i in 1:nrow(mat.all)) { > start.year <- min(which(is.na(mat.all[i,])==F & (mat.all[i,]<0))) > fin.year <- max(which(is.na(mat.all[i,])==F)) > for(j in start.year:fin.year) { > if(mat.all[i,(j-int):(j-1)]<0){ > mat.s[i,j] <- ifelse((mat.all[i,(j-int):(j-1)])==pmin, mat.all[i,j], > 0); > mat.r [i,j] <- which.max((mat.all[i,(j+1):(j+int)]) > 0) > } > }} > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
Reasonably Related Threads
- R: Re: chol( neg.def.matrix ) WAS: Re: Choleski and Choleski with pivoting of matrix fails
- R: Re: R: Re: chol( neg.def.matrix ) WAS: Re: Choleski and Choleski with pivoting of matrix fails
- ltext - adding text to each panel from a matrix
- [ifelse] how to maintain a value from original matrix without probs?
- geom_edge & color