Re: [R] loop problem
On Oct 2, 2009, at 7:34 AM, crenial30 wrote: Thanks a lot David for your answer. I am sorry for being so minimal. I wanted to produce a list/vector/table consisting each vector produced from this code len<-20 for (n1 in seq(0,(len-1),by=1)){ f <- R_event[R_event > (rx[1]+ n1*300)& R_event <= (rx[1] + 300*(n1+1))] //create list for each values of n1 That throws an error: f <- R_event[R_event > (rx[1]+ n1*300)& R_event <= (rx[1] + 300*(n1+1))] + //create list for each values of n1 Error: syntax error Plus... even if it were syntactically correct, we are not going to know what R_event is. Try posting code that creates a data situation and then express in English (not mangled R) what you want to happen. R_ev_char <- as.character(f) lis01 <- c(length(R_ev_char)) // here I want to store the values of lengths of R_ev_char vector for successive values of n1(0,1,2,3,...20) I already tried with vector notation but no help. I just want to store Lengths on each successive integer so that I can draw the list on 300(secs) interval. Any suggestion would be very much helpful. crenial crenial30 wrote: I have the following code which I wanted to convert using for loop previous code: R_ev1 <- R_event[R_event > (rx[1] + 300*0)& R_event <= (rx[1] + 300*1)] R_ev2 <- R_event[R_event > (rx[1] + 300*1)& R_event <= (rx[1] + 300*2)] R_ev3 <- R_event[R_event > (rx[1] + 300*2)& R_event <= (rx[1] + 300*3)] R_ev4 <- R_event[R_event > (rx[1] + 300*3)& R_event <= (rx[1] + 300*4)] R_ev5 <- R_event[R_event > (rx[1] + 300*4)& R_event <= (rx[1] + 300*5)] R_ev6 <- R_event[R_event > (rx[1] + 300*5)& R_event <= (rx[1] + 300*6)] R_ev7 <- R_event[R_event > (rx[1] + 300*6)& R_event <= (rx[1] + 300*7)] R_ev8 <- R_event[R_event > (rx[1] + 300*7)& R_event <= (rx[1] + 300*8)] R_ev9 <- R_event[R_event > (rx[1] + 300*8)& R_event <= (rx[1] + 300*9)] R_ev10 <- R_event[R_event > (rx[1] + 300*9)& R_event <= (rx[1] + 300*10)] R_ev11 <- R_event[R_event > (rx[1] + 300*10)& R_event <= (rx[1] + 300*11)] R_ev12 <- R_event[R_event > (rx[1] + 300*11)& R_event <= (rx[1] + 300*12)] R_ev13 <- R_event[R_event > (rx[1] + 300*12)& R_event <= (rx[1] + 300*13)] R_ev14 <- R_event[R_event > (rx[1] + 300*13)& R_event <= (rx[1] + 300*14)] R_ev15 <- R_event[R_event > (rx[1] + 300*14)& R_event <= (rx[1] + 300*15)] R_ev16 <- R_event[R_event > (rx[1] + 300*15)& R_event <= (rx[1] + 300*16)] R_ev1c <- as.character(R_ev1) R_ev2c <- as.character(R_ev2) R_ev3c <- as.character(R_ev3) R_ev4c <- as.character(R_ev4) R_ev5c <- as.character(R_ev5) R_ev6c <- as.character(R_ev6) R_ev7c <- as.character(R_ev7) R_ev8c <- as.character(R_ev8) R_ev9c <- as.character(R_ev9) R_ev10c <- as.character(R_ev10) R_ev11c <- as.character(R_ev11) R_ev12c <- as.character(R_ev12) R_ev13c <- as.character(R_ev13) R_ev14c <- as.character(R_ev14) R_ev15c <- as.character(R_ev15) R_ev16c <- as.character(R_ev16) lis1 <- c( length(R_ev1c), length(R_ev2c), length(R_ev3c), length(R_ev4c), length(R_ev5c), length(R_ev6c), length(R_ev7c), length(R_ev8c), length(R_ev9c), length(R_ev10c), length(R_ev11c), length(R_ev12c), length(R_ev13c), length(R_ev14c), length(R_ev15c), length(R_ev16c) ) # R_ev1_mat <- matrix(unlist(R_ev1),nrow=T) barplot(lis1, angle = 15+10*1:5, density = 20, col = "black", legend = rownames(lis1)) title(main = list("Number of Occurred Received events in every 5 mins", font = 4)) # dev.off() New code: f1<- seq(rx[1],rx[2],by=300) f<- as.character(f1) len <- length(f) for (n1 in 0:(len-1)){ for(n2 in 1:len){ # R_ev_01 <- R_event[R_event <= (rx[1] + 300)] R_ev[n1] <- R_event[R_event > (rx[1]+ n1*300)& R_event <= (rx[1] + 300*n2)] R_ev_char <- as.character(R_ev) lis01 <- list(length(R_ev_char)) barplot(lis01, angle = 15+10*1:5, density = 20, col = "blue", legend = rownames(lis01)) title(main = list("Number of Occurred Received Events in every 5 mins", font = 4)) } } Somehow it is not working and giving an error saying "Fehler in -0.01 * height : nicht-numerisches Argument für binären Operator". Any help would be really appreciated. Thanks Crenial -- View this message in context: http://www.nabble.com/loop-problem-tp25665329p25714305.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] loop problem
Thanks a lot David for your answer. I am sorry for being so minimal. I wanted to produce a list/vector/table consisting each vector produced from this code len<-20 for (n1 in seq(0,(len-1),by=1)){ f <- R_event[R_event > (rx[1]+ n1*300)& R_event <= (rx[1] + 300*(n1+1))] //create list for each values of n1 R_ev_char <- as.character(f) lis01 <- c(length(R_ev_char)) // here I want to store the values of lengths of R_ev_char vector for successive values of n1(0,1,2,3,...20) I already tried with vector notation but no help. I just want to store Lengths on each successive integer so that I can draw the list on 300(secs) interval. Any suggestion would be very much helpful. crenial crenial30 wrote: > > I have the following code which I wanted to convert using for loop > > previous code: > R_ev1 <- R_event[R_event > (rx[1] + 300*0)& R_event <= (rx[1] + 300*1)] > R_ev2 <- R_event[R_event > (rx[1] + 300*1)& R_event <= (rx[1] + 300*2)] > R_ev3 <- R_event[R_event > (rx[1] + 300*2)& R_event <= (rx[1] + 300*3)] > R_ev4 <- R_event[R_event > (rx[1] + 300*3)& R_event <= (rx[1] + 300*4)] > > R_ev5 <- R_event[R_event > (rx[1] + 300*4)& R_event <= (rx[1] + 300*5)] > R_ev6 <- R_event[R_event > (rx[1] + 300*5)& R_event <= (rx[1] + 300*6)] > R_ev7 <- R_event[R_event > (rx[1] + 300*6)& R_event <= (rx[1] + 300*7)] > R_ev8 <- R_event[R_event > (rx[1] + 300*7)& R_event <= (rx[1] + 300*8)] > > R_ev9 <- R_event[R_event > (rx[1] + 300*8)& R_event <= (rx[1] + 300*9)] > R_ev10 <- R_event[R_event > (rx[1] + 300*9)& R_event <= (rx[1] + 300*10)] > R_ev11 <- R_event[R_event > (rx[1] + 300*10)& R_event <= (rx[1] + 300*11)] > R_ev12 <- R_event[R_event > (rx[1] + 300*11)& R_event <= (rx[1] + 300*12)] > > R_ev13 <- R_event[R_event > (rx[1] + 300*12)& R_event <= (rx[1] + 300*13)] > R_ev14 <- R_event[R_event > (rx[1] + 300*13)& R_event <= (rx[1] + 300*14)] > R_ev15 <- R_event[R_event > (rx[1] + 300*14)& R_event <= (rx[1] + 300*15)] > R_ev16 <- R_event[R_event > (rx[1] + 300*15)& R_event <= (rx[1] + 300*16)] > > > R_ev1c <- as.character(R_ev1) > R_ev2c <- as.character(R_ev2) > R_ev3c <- as.character(R_ev3) > R_ev4c <- as.character(R_ev4) > R_ev5c <- as.character(R_ev5) > R_ev6c <- as.character(R_ev6) > R_ev7c <- as.character(R_ev7) > R_ev8c <- as.character(R_ev8) > > R_ev9c <- as.character(R_ev9) > R_ev10c <- as.character(R_ev10) > R_ev11c <- as.character(R_ev11) > R_ev12c <- as.character(R_ev12) > R_ev13c <- as.character(R_ev13) > R_ev14c <- as.character(R_ev14) > R_ev15c <- as.character(R_ev15) > R_ev16c <- as.character(R_ev16) > > > > > lis1 <- c( > length(R_ev1c), > length(R_ev2c), > length(R_ev3c), > length(R_ev4c), > length(R_ev5c), > length(R_ev6c), > length(R_ev7c), > length(R_ev8c), > length(R_ev9c), > length(R_ev10c), > length(R_ev11c), > length(R_ev12c), > length(R_ev13c), > length(R_ev14c), > length(R_ev15c), > length(R_ev16c) > ) > > > # R_ev1_mat <- matrix(unlist(R_ev1),nrow=T) > > > barplot(lis1, angle = 15+10*1:5, density = 20, col = "black", >legend = rownames(lis1)) > title(main = list("Number of Occurred Received events in every 5 mins", > font = 4)) > > # dev.off() > > > New code: > > f1<- seq(rx[1],rx[2],by=300) > f<- as.character(f1) > len <- length(f) > > for (n1 in 0:(len-1)){ > for(n2 in 1:len){ > # R_ev_01 <- R_event[R_event <= (rx[1] + 300)] > > R_ev[n1] <- R_event[R_event > (rx[1]+ n1*300)& R_event <= (rx[1] + > 300*n2)] > R_ev_char <- as.character(R_ev) > > lis01 <- list(length(R_ev_char)) > barplot(lis01, angle = 15+10*1:5, density = 20, col = "blue", >legend = rownames(lis01)) > title(main = list("Number of Occurred Received Events in every 5 mins", > font = 4)) > } > } > > Somehow it is not working and giving an error saying "Fehler in -0.01 * > height : nicht-numerisches Argument für binären Operator". > > Any help would be really appreciated. > > Thanks > Crenial > -- View this message in context: http://www.nabble.com/loop-problem-tp25665329p25714305.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] loop problem
That code is way more than a _minimal_ example, and its not _reproducible_ either, so just a comment: Have you considered creating a vector rather than separate "event" variables? R_ev[1:16] <- R_event > (rx[1] + 300*(0:15)& R_event <= (rx[1] + 300*(1:16) ?cut # would also appear to be a function that might offer advantages -- David On Sep 29, 2009, at 11:34 AM, crenial30 wrote: I have the following code which I wanted to convert using for loop previous code: R_ev1 <- R_event[R_event > (rx[1] + 300*0)& R_event <= (rx[1] + 300*1)] R_ev2 <- R_event[R_event > (rx[1] + 300*1)& R_event <= (rx[1] + 300*2)] R_ev3 <- R_event[R_event > (rx[1] + 300*2)& R_event <= (rx[1] + 300*3)] R_ev4 <- R_event[R_event > (rx[1] + 300*3)& R_event <= (rx[1] + 300*4)] R_ev5 <- R_event[R_event > (rx[1] + 300*4)& R_event <= (rx[1] + 300*5)] R_ev6 <- R_event[R_event > (rx[1] + 300*5)& R_event <= (rx[1] + 300*6)] R_ev7 <- R_event[R_event > (rx[1] + 300*6)& R_event <= (rx[1] + 300*7)] R_ev8 <- R_event[R_event > (rx[1] + 300*7)& R_event <= (rx[1] + 300*8)] R_ev9 <- R_event[R_event > (rx[1] + 300*8)& R_event <= (rx[1] + 300*9)] R_ev10 <- R_event[R_event > (rx[1] + 300*9)& R_event <= (rx[1] + 300*10)] R_ev11 <- R_event[R_event > (rx[1] + 300*10)& R_event <= (rx[1] + 300*11)] R_ev12 <- R_event[R_event > (rx[1] + 300*11)& R_event <= (rx[1] + 300*12)] R_ev13 <- R_event[R_event > (rx[1] + 300*12)& R_event <= (rx[1] + 300*13)] R_ev14 <- R_event[R_event > (rx[1] + 300*13)& R_event <= (rx[1] + 300*14)] R_ev15 <- R_event[R_event > (rx[1] + 300*14)& R_event <= (rx[1] + 300*15)] R_ev16 <- R_event[R_event > (rx[1] + 300*15)& R_event <= (rx[1] + 300*16)] R_ev1c <- as.character(R_ev1) R_ev2c <- as.character(R_ev2) R_ev3c <- as.character(R_ev3) R_ev4c <- as.character(R_ev4) R_ev5c <- as.character(R_ev5) R_ev6c <- as.character(R_ev6) R_ev7c <- as.character(R_ev7) R_ev8c <- as.character(R_ev8) R_ev9c <- as.character(R_ev9) R_ev10c <- as.character(R_ev10) R_ev11c <- as.character(R_ev11) R_ev12c <- as.character(R_ev12) R_ev13c <- as.character(R_ev13) R_ev14c <- as.character(R_ev14) R_ev15c <- as.character(R_ev15) R_ev16c <- as.character(R_ev16) lis1 <- c( length(R_ev1c), length(R_ev2c), length(R_ev3c), length(R_ev4c), length(R_ev5c), length(R_ev6c), length(R_ev7c), length(R_ev8c), length(R_ev9c), length(R_ev10c), length(R_ev11c), length(R_ev12c), length(R_ev13c), length(R_ev14c), length(R_ev15c), length(R_ev16c) ) # R_ev1_mat <- matrix(unlist(R_ev1),nrow=T) barplot(lis1, angle = 15+10*1:5, density = 20, col = "black", legend = rownames(lis1)) title(main = list("Number of Occurred Received events in every 5 mins", font = 4)) # dev.off() New code: f1<- seq(rx[1],rx[2],by=300) f<- as.character(f1) len <- length(f) for (n1 in 0:(len-1)){ for(n2 in 1:len){ # R_ev_01 <- R_event[R_event <= (rx[1] + 300)] R_ev[n1] <- R_event[R_event > (rx[1]+ n1*300)& R_event <= (rx[1] + 300*n2)] R_ev_char <- as.character(R_ev) lis01 <- list(length(R_ev_char)) barplot(lis01, angle = 15+10*1:5, density = 20, col = "blue", legend = rownames(lis01)) title(main = list("Number of Occurred Received Events in every 5 mins", font = 4)) } } Somehow it is not working and giving an error saying "Fehler in -0.01 * height : nicht-numerisches Argument für binären Operator". Any help would be really appreciated. Thanks Crenial -- View this message in context: http://www.nabble.com/loop-problem-tp25665329p25665329.html Sent from the R help mailing list archive at Nabble.com. __ 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. David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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.
Re: [R] loop problem for extract coefficients
Hi Alex, On Sun, April 5, 2009 16:49, Alex Roy wrote: > Dear R users, > I have problem with extracting coefficients from a > object. Here, X (predictor)and Y (response) are two matrix , I am > regressing > X ( dimensions 10 x 20) on each of columns of Y[,1] (10 x 1) and want to > store the coefficient values. I have performed a Elastic Net regression > and > I want to store the coeffcients in each iteration. I got an error message > . > I do not know where is the problem Please help me. > > Thanks > > > > *Code:* > > --- > library(elasticnet) > X<-matrix(rnorm(200),ncol=20) > Y<-matrix(rnorm(200),ncol=20) > loop <- 20 > size <- 20 > enres<-matrix(nrow = size, ncol = loop) > fit<-matrix(nrow = size, ncol = loop) > store<-matrix(nrow = size, ncol = loop) > for(j in 1: 10) > print (paste(j,"/200",sep="")) > { > enres<-enet(x=X,y=Y[,j],lambda=1,normalize=TRUE,intercept=TRUE) > fit<-predict.enet(enres, X, type="coefficients") > store[,j]<-fit$coefficients > } > The problem is that only the print statement is inside the for loop. I would suggest: for(j in 1: 10) { # here the opening bracket print (paste(j,"/200",sep="")) # and not here! enres<-enet(x=X,y=Y[,j],lambda=1,normalize=TRUE,intercept=TRUE) fit<-predict.enet(enres, X, type="coefficients") store[,j]<-fit$coefficients } Hope this helps, Arien > >> library(elasticnet) > Loading required package: lars >> X<-matrix(rnorm(200),ncol=20) >> Y<-matrix(rnorm(200),ncol=20) >> >> loop <- 20 >> size <- 20 >> >> enres<-matrix(nrow = size, ncol = loop) >> fit<-matrix(nrow = size, ncol = loop) >> store<-matrix(nrow = size, ncol = loop) >> >> for(j in 1: 10) > + print (paste(j,"/200",sep="")) > [1] "1/200" > [1] "2/200" > [1] "3/200" > [1] "4/200" > [1] "5/200" > [1] "6/200" > [1] "7/200" > [1] "8/200" > [1] "9/200" > [1] "10/200" >> { > + enres<-enet(x=X,y=Y[,j],lambda=1,normalize=TRUE,intercept=TRUE) > + fit<-predict.enet(enres, X, type="coefficients") > + store[,j]<-fit$coefficients > + } > *Error in store[, j] <- fit$coefficients : > number of items to replace is not a multiple of replacement length >> * > > [[alternative HTML version deleted]] > > __ > 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. > -- drs. H.A. (Arien) Lam (Ph.D. student) Department of Physical Geography Faculty of Geosciences Utrecht University, The Netherlands __ 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.
Re: [R] loop problem for extract coefficients
Perhaps your loop should be more than just a print statement. That works fine! You need to place the print statement after the '{', not before it. fit$coefficients is a 21 x 20 array (the rows are lablelled 0 to 20) and you are trying to put it in the jth *column* of a 20 x 20 matrix. Not surprisingly it does not fit. Perhaps you need to know more about just what it is this software is doing. Bill Venables http://www.cmis.csiro.au/bill.venables/ -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Alex Roy Sent: Monday, 6 April 2009 12:49 AM To: r-help@r-project.org Subject: [R] loop problem for extract coefficients Dear R users, I have problem with extracting coefficients from a object. Here, X (predictor)and Y (response) are two matrix , I am regressing X ( dimensions 10 x 20) on each of columns of Y[,1] (10 x 1) and want to store the coefficient values. I have performed a Elastic Net regression and I want to store the coeffcients in each iteration. I got an error message . I do not know where is the problem Please help me. Thanks *Code:* --- library(elasticnet) X<-matrix(rnorm(200),ncol=20) Y<-matrix(rnorm(200),ncol=20) loop <- 20 size <- 20 enres<-matrix(nrow = size, ncol = loop) fit<-matrix(nrow = size, ncol = loop) store<-matrix(nrow = size, ncol = loop) for(j in 1: 10) print (paste(j,"/200",sep="")) { enres<-enet(x=X,y=Y[,j],lambda=1,normalize=TRUE,intercept=TRUE) fit<-predict.enet(enres, X, type="coefficients") store[,j]<-fit$coefficients } > library(elasticnet) Loading required package: lars > X<-matrix(rnorm(200),ncol=20) > Y<-matrix(rnorm(200),ncol=20) > > loop <- 20 > size <- 20 > > enres<-matrix(nrow = size, ncol = loop) > fit<-matrix(nrow = size, ncol = loop) > store<-matrix(nrow = size, ncol = loop) > > for(j in 1: 10) + print (paste(j,"/200",sep="")) [1] "1/200" [1] "2/200" [1] "3/200" [1] "4/200" [1] "5/200" [1] "6/200" [1] "7/200" [1] "8/200" [1] "9/200" [1] "10/200" > { + enres<-enet(x=X,y=Y[,j],lambda=1,normalize=TRUE,intercept=TRUE) + fit<-predict.enet(enres, X, type="coefficients") + store[,j]<-fit$coefficients + } *Error in store[, j] <- fit$coefficients : number of items to replace is not a multiple of replacement length > * [[alternative HTML version deleted]] __ 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.
Re: [R] loop problem
Muhammad Azam wrote: Dear R members I have a problem regarding storing the lists. Let L=number of distinct values of any predictor (say L=5) P=number of predictors (say P=20) g1 <- c() for(i in 1:P){ if(L > 1){ for(j in 1:(L-1)){ g <- g1[j] <- g } } g2[]=sort.list(g1) } Now the question is: What should we use inside brackets of g2[], whether "i" or some thing else? If L is not greater than 1 then there will be a "NULL" for g2. We don't want to store it in g2, so how can we handle this problem. Looking forward for some help. Thanks and Hi Muhammad, The first thing I would ask is "Why not store NULL in g2[[i]] if that element is NULL?". If you do this: for(i in 1:P) { if(L > 1) { g1<-rep(NA,L) for(j in 1:(L-1)) g1[j]<-... g2[[i]]<-sort.list(g1) } } you will have fewer elements in g2 than you have sets of predictors and will then have to match up the sets of predictors with the elements in g2. I think you could just test for NULL is.null(g2[[i]]) later on if you don't want to process these in a subsequent step. Jim __ 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.
Re: [R] Loop problem
Try this: pk[rep(1, 4), ] On Mon, Jul 14, 2008 at 4:33 AM, fernanda lopez <[EMAIL PROTECTED]> wrote: > Dear all, > I want to > write ck<-cbind(rep(pk[1,1],4),rep(pk[1,2],4),rep(pk[1,3],4)) state ment > in a loop . How can I write it ? > > > > > >> pk > [,1] [,2] [,3] > [1,] -1.1354816 0.9808877 -0.9446314 > [2,] 0.7787378 0.4536944 0.3204882 > [3,] -1.7274907 1.5112011 1.4481839 > [4,] 1.0629145 0.5976109 -0.5277638 > > > >> pk<-matrix(rnorm(12),nrow=4,ncol=3) >> pk > [,1] [,2] [,3] > [1,] -1.1354816 0.9808877 -0.9446314 > [2,] 0.7787378 0.4536944 0.3204882 > [3,] -1.7274907 1.5112011 1.4481839 > [4,] 1.0629145 0.5976109 -0.5277638 > >> ck<-cbind(rep(pk[1,1],4),rep(pk[1,2],4),rep(pk[1,3],4)) >> ck > [,1] [,2] [,3] > [1,] -1.135482 0.9808877 -0.9446314 > [2,] -1.135482 0.9808877 -0.9446314 > [3,] -1.135482 0.9808877 -0.9446314 > [4,] -1.135482 0.9808877 -0.9446314 > > > Thanks for your help > > Fernanda Lopez > Netherlands > >[[alternative HTML version deleted]] > > __ > 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.
Re: [R] Loop problem
Hi, do you mean this? pk<-matrix(rnorm(12),nrow=4,ncol=3) ck1<-cbind(rep(pk[,1],4),rep(pk[,2],4),rep(pk[,3],4)) ## or ck2<-cbind(rep(pk[,1],each=4),rep(pk[,2],each=4),rep(pk[,3],each=4)) pk ck1 ck2 best, Daniel fernanda lopez wrote: > > Dear all, > I want to > write ck<-cbind(rep(pk[1,1],4),rep(pk[1,2],4),rep(pk[1,3],4)) state > ment > in a loop . How can I write it ? > > > > > >> pk >[,1] [,2] [,3] > [1,] -1.1354816 0.9808877 -0.9446314 > [2,] 0.7787378 0.4536944 0.3204882 > [3,] -1.7274907 1.5112011 1.4481839 > [4,] 1.0629145 0.5976109 -0.5277638 > > > >> pk<-matrix(rnorm(12),nrow=4,ncol=3) >> pk >[,1] [,2] [,3] > [1,] -1.1354816 0.9808877 -0.9446314 > [2,] 0.7787378 0.4536944 0.3204882 > [3,] -1.7274907 1.5112011 1.4481839 > [4,] 1.0629145 0.5976109 -0.5277638 > >> ck<-cbind(rep(pk[1,1],4),rep(pk[1,2],4),rep(pk[1,3],4)) >> ck > [,1] [,2] [,3] > [1,] -1.135482 0.9808877 -0.9446314 > [2,] -1.135482 0.9808877 -0.9446314 > [3,] -1.135482 0.9808877 -0.9446314 > [4,] -1.135482 0.9808877 -0.9446314 > > > Thanks for your help > > Fernanda Lopez > Netherlands > > [[alternative HTML version deleted]] > > __ > 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. > > -- View this message in context: http://www.nabble.com/Loop-problem-tp18439649p18439918.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Loop problem
try this: pk <- matrix(rnorm(12), 4, 3) matrix(rep(pk[1, ], each = 4), 4) I hope it helps. Best, Dimitris Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm - Original Message - From: "fernanda lopez" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Monday, July 14, 2008 10:33 AM Subject: [R] Loop problem Dear all, I want to write ck<-cbind(rep(pk[1,1],4),rep(pk[1,2],4),rep(pk[1,3],4)) state ment in a loop . How can I write it ? pk [,1] [,2] [,3] [1,] -1.1354816 0.9808877 -0.9446314 [2,] 0.7787378 0.4536944 0.3204882 [3,] -1.7274907 1.5112011 1.4481839 [4,] 1.0629145 0.5976109 -0.5277638 pk<-matrix(rnorm(12),nrow=4,ncol=3) pk [,1] [,2] [,3] [1,] -1.1354816 0.9808877 -0.9446314 [2,] 0.7787378 0.4536944 0.3204882 [3,] -1.7274907 1.5112011 1.4481839 [4,] 1.0629145 0.5976109 -0.5277638 ck<-cbind(rep(pk[1,1],4),rep(pk[1,2],4),rep(pk[1,3],4)) ck [,1] [,2] [,3] [1,] -1.135482 0.9808877 -0.9446314 [2,] -1.135482 0.9808877 -0.9446314 [3,] -1.135482 0.9808877 -0.9446314 [4,] -1.135482 0.9808877 -0.9446314 Thanks for your help Fernanda Lopez Netherlands [[alternative HTML version deleted]] __ 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. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ 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.
Re: [R] Loop problem
Oops, missed the first line of the example: storm.data<-data.frame(meas.index=10001:2,cumrain=runif(1,-10,10)+10) You can generalize the function to whatever column names you have like this: find.max.rain<-function(raindata,howmany,raincol) { # a lazy way of getting the same structure as raindata # it assumes that raindata has the data structure that you want maxrain<-raindata[1:howmany,] for(i in 1:howmany) { raindim<-dim(raindata) thismax<-which.max(raindata[[raincol]]) cat(thismax," ") maxrain[i,]<-raindata[thismax,] mindex<-thismax-48 if(mindex < 1) mindex <- 1 maxdex<-thismax+48 cat(mindex,maxdex," ") if(maxdex > raindim[1]) maxdex<-raindim[1] raindata<-raindata[-(mindex:maxdex),] cat(raindim[1]," ") } cat("\n") return(maxrain) } find.max.rain(storm.data,50,"cumrain") Jim __ 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.
Re: [R] Loop problem
Jamie Ledingham wrote: > Dear all, I have a problem with a loop, if anyone has any knowledge on > these things I would appreciate some comments. The code below is > designed to allow me to extract the top record of the data frame, and > them remove rows from the data frame which have an index close to the > extracted top record. > > > topstorm<-subset(rankeddataset[1,]) ## Extracts the top storm > topstormindex<-rankeddataset[1,1] ## Finds the top storm start index > startindex<-topstormindex-48 ## sets the start and end indexes > endindex<-topstorminde+48 > rankeddataset<-rankeddataset[-1,] ## Creates a new list with the top > storm removed > > ##This section of code needs looped. It removes storms from the list > which are too close to the extracted storm > > for (i in 1:30){ > if (rankeddataset[i,1]>startindex && rankeddataset[i,1] {rankeddataset<-rankeddataset[-i,]} > } > > Here is some example data: > >82856 15 / 6 / 1966 82856:8287925.9 >82857 15 / 6 / 1966 82857:8288020.5 >83036 23 / 6 / 1966 83036:8305917.3 >87250 15 / 12 / 1966 87250:8727315.9 > Hi again Jamie, I had a bit of time tonight and recognized your problem. I think the following function will do what you want, although it is probably not the most elegant solution. I would check it manually with a small data file and a small "howmany" argument to make sure that it is picking up the maxima you want. find.max.rain<-function(raindata,howmany) { # a lazy way of getting the same structure as raindata # it assumes that raindata has the data structure that you want maxrain<-raindata[1:howmany,] for(i in 1:howmany) { # get the current number of rows nrows<-dim(raindata)[1] # find the first maximum value thismax<-which.max(raindata$cumrain) # transfer it to the return data frame maxrain[i,]<-raindata[thismax,] # calculate the minimum index for the 48 hour gap mindex<-thismax-48 # make sure it is at least 1 if(mindex < 1) mindex <- 1 # calculate the maximum index for the gap maxdex<-thismax+48 # make sure it doesn't go past the end of the data frame if(maxdex > nrows) maxdex<-nrows # chop out that time period raindata<-raindata[-(mindex:maxdex),] } return(maxrain) } Jim __ 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.
Re: [R] Loop problem
Basically you are moving the data up and then incrementing to the next row. Here is an example; assume that you are at the 2nd entry: 1 2 <== here 3 4 Now your loop index is 2 and you remove the current data ('2') and are left with: 1 3 <== index of 2 points here 4 Now you increment the index to 3 and you get 1 3 4 <== now here So you have skipped 3. What you should be doing is determining all the index values that meet your criteria and then deleting them all at once, or start from the bottom of the dataframe and work up. On 3/26/08, Jamie Ledingham <[EMAIL PROTECTED]> wrote: > Dear all, I have a problem with a loop, if anyone has any knowledge on > these things I would appreciate some comments. The code below is > designed to allow me to extract the top record of the data frame, and > them remove rows from the data frame which have an index close to the > extracted top record. > > > topstorm<-subset(rankeddataset[1,]) ## Extracts the top storm > topstormindex<-rankeddataset[1,1] ## Finds the top storm start index > startindex<-topstormindex-48 ## sets the start and end indexes > endindex<-topstorminde+48 > rankeddataset<-rankeddataset[-1,] ## Creates a new list with the top > storm removed > > ##This section of code needs looped. It removes storms from the list > which are too close to the extracted storm > > for (i in 1:30){ > if (rankeddataset[i,1]>startindex && rankeddataset[i,1] {rankeddataset<-rankeddataset[-i,]} > } > > Here is some example data: > > 82856 15 / 6 / 1966 82856:8287925.9 > 82857 15 / 6 / 1966 82857:8288020.5 > 83036 23 / 6 / 1966 83036:8305917.3 > 87250 15 / 12 / 1966 87250:8727315.9 > > The loop does not currently work, it seems to remove every second line > or so. Can anyone suggest why this might be, I'm not particularly > experienced in using loops so it may be a rookie mistake. Thanks in > advance. > Jamie Ledingham > > __ > 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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ 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.