Re: [R] for loop not working
Thank you for your comments. I guess what I need is to store movements and then loop through that matrix. Here is a snippet of the 'movement' code. I am trying to fill the matrix below with data from this loop. Is there something else my matrix (bottom) requires? Or its positioning? Many thanks, # determine whether movement is occurring and if so what type # if there are insufficient history items then just record movement types discretely if (i < historyLength) movement <- movementType(relAngle, angleThreshold) else if (i > historyLength-1) { # Array to store speeds speedHistory <- array(historyLength) n = historyLength-1 # get the speeds from the previous n (hisoryLength) "Movements" for (j in seq(1, length(historyLength))){ speedHistory [n] = R[i-j, 6] n-1 } if (!bayesFilter(speedHistory, minSpeed, GPS_accy)) movement <- "non-moving" else if(bayesFilter(speedHistory, minSpeed, GPS_accy)) movement <- movementType(relAngle, angleThreshold) holder <- matrix(data = movement, nrow = nrow(R), ncol = 1) } -- View this message in context: http://r.789695.n4.nabble.com/for-loop-not-working-tp4713392p4713525.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] for loop not working
In addition to Jim's comments you would benefit by learning more about how R works. You are using loops for operations that could easily be vectorized. It is not clear where your functions are coming from (eg. deg2rad and gcd.vif) but package geosphere will get your distances and bearings directly without looping. > install.packages("geosphere") > library(geosphere) > Cities <- c("New York", "Miami", "Los Angeles", "Seattle", "Chicago") > Lat <- c(40.7127, 25.7753, 34.05, 47.6097, 41.8369) > Long <- c(-74.0059, -80.2089, -118.250, -122.3331, -87.6847) > Places <- data.frame(Cities, Lat, Long) > Places Cities Lat Long 1New York 40.7127 -74.0059 2 Miami 25.7753 -80.2089 3 Los Angeles 34.0500 -118.2500 4 Seattle 47.6097 -122.3331 5 Chicago 41.8369 -87.6847 > n <- nrow(Places) > distVincentyEllipsoid(Places[1:(n-1), 3:2], Places[2:n, 3:2]) [1] 1753420 3763369 1544119 2794013 > bearing(Places[1:(n-1), 3:2], Places[2:n, 3:2]) [1] -158.98140 -66.71221 -11.57217 90.36231 David L. Carlson Department of Anthropology Texas A University -Original Message- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Jim Lemon Sent: Saturday, October 10, 2015 6:04 AM To: mnw <ma...@aber.ac.uk> Cc: r-help mailing list <r-help@r-project.org> Subject: Re: [R] for loop not working Hi mnw, It looks to me as though you are testing the entire "holder" list each time you go through the function. First, I'm not sure that a simple "==" is the test you want. "movement" seems to be more than a single value and so you might want to write a function that tests whether all of the components of two "movement" objects are equal e.g.: move_change<-function(currentx,previousx) { if(currentx$speed == previousx$speed) { if(currentx$heading == previousx$heading) return(0) } return(1) } Second, you probably don't want to test the entire list each time you loop through the function. Just use: if(i > 1) changes<-move_change(movement,holder) holder<-movement Jim On Sat, Oct 10, 2015 at 2:08 AM, mnw <ma...@aber.ac.uk> wrote: > Hi. I have some code which loops through raw GPS data. This initial loop > calculates distance, angle, speed etc between successive coordinates as > well > as type of movement e.g.left, right, forward. I want to construct a second > loop that goes through the movements and records 'Changes' as either '0' or > '1' depending on whether the movement changed or not. I have put the whole > code in for reference but the additional loop begins at the object > 'holder.' > I want to store movements in holder and then loop through holder to > determine 'changes.' At the moment it gives me 'Error in holder[[t - 1]] : > attempt to select less than one element.' The moment i make holder [[t]] it > works but just gives a list of '0's. I have tried many different things and > just cannot get it to work, so any help would be very much appreciated. > Again, sorry for the long post. > > > > > lst <- list() # temporary list to store the results > for (i in seq(1, nrow(R) - 1)){ # loop through each row of the 'R' matrix > lat1 <- deg2rad(R[i, 4]) > long1 <- deg2rad(R[i, 5]) > lat2 <- deg2rad(R[i + 1, 4]) > long2 <- deg2rad(R[i + 1, 5]) > gcd_vif <- gcd.vif(lat1, long1, lat2, long2) > > # calc estimated speed by mean of speeds between two GPS records > estSpeed <- (R[i, 6] + R[i+1, 6])/2 > > # calculate acceleration (velocity) > accel <- (R[i+1, 6] - R[i, 6]) / GPSSample > > # calculate absolute heading > heading <- absoluteHeading(lat1, long1, lat2, long2) > > # calculate bearing relative to previous GPS record > relAngle <- 0 > # if the number of GPS records is less than 3 then no change in track > if (i < 1) relAngle = heading > # otherwise consider the previous heading in order to calculate the new > track > else if (i > 0) { > relAngle = (previousHeading - heading) > > } > > > # determine whether movement is occurring and if so what type > # if there are insufficient history items then just record movement types > discretely >if (i < historyLength) movement <- movementType(relAngle, > angleThreshold) > > else if (i > historyLength-1) { > # Array to store speeds > speedHistory <- array(historyLength) > n = historyLength-1 > # get the speeds from the previous n (hisoryLength) "Movements" > for (j in seq(1, length(historyLength))){ > speedHistory [n] = R[i-j, 6] > n-1 > } > > if (!bayesFilter(speedHistory, minSpeed, GPS_accy)) movement <- > "non-m
Re: [R] for loop not working
Hi mnw, It looks to me as though you are testing the entire "holder" list each time you go through the function. First, I'm not sure that a simple "==" is the test you want. "movement" seems to be more than a single value and so you might want to write a function that tests whether all of the components of two "movement" objects are equal e.g.: move_change<-function(currentx,previousx) { if(currentx$speed == previousx$speed) { if(currentx$heading == previousx$heading) return(0) } return(1) } Second, you probably don't want to test the entire list each time you loop through the function. Just use: if(i > 1) changes<-move_change(movement,holder) holder<-movement Jim On Sat, Oct 10, 2015 at 2:08 AM, mnwwrote: > Hi. I have some code which loops through raw GPS data. This initial loop > calculates distance, angle, speed etc between successive coordinates as > well > as type of movement e.g.left, right, forward. I want to construct a second > loop that goes through the movements and records 'Changes' as either '0' or > '1' depending on whether the movement changed or not. I have put the whole > code in for reference but the additional loop begins at the object > 'holder.' > I want to store movements in holder and then loop through holder to > determine 'changes.' At the moment it gives me 'Error in holder[[t - 1]] : > attempt to select less than one element.' The moment i make holder [[t]] it > works but just gives a list of '0's. I have tried many different things and > just cannot get it to work, so any help would be very much appreciated. > Again, sorry for the long post. > > > > > lst <- list() # temporary list to store the results > for (i in seq(1, nrow(R) - 1)){ # loop through each row of the 'R' matrix > lat1 <- deg2rad(R[i, 4]) > long1 <- deg2rad(R[i, 5]) > lat2 <- deg2rad(R[i + 1, 4]) > long2 <- deg2rad(R[i + 1, 5]) > gcd_vif <- gcd.vif(lat1, long1, lat2, long2) > > # calc estimated speed by mean of speeds between two GPS records > estSpeed <- (R[i, 6] + R[i+1, 6])/2 > > # calculate acceleration (velocity) > accel <- (R[i+1, 6] - R[i, 6]) / GPSSample > > # calculate absolute heading > heading <- absoluteHeading(lat1, long1, lat2, long2) > > # calculate bearing relative to previous GPS record > relAngle <- 0 > # if the number of GPS records is less than 3 then no change in track > if (i < 1) relAngle = heading > # otherwise consider the previous heading in order to calculate the new > track > else if (i > 0) { > relAngle = (previousHeading - heading) > > } > > > # determine whether movement is occurring and if so what type > # if there are insufficient history items then just record movement types > discretely >if (i < historyLength) movement <- movementType(relAngle, > angleThreshold) > > else if (i > historyLength-1) { > # Array to store speeds > speedHistory <- array(historyLength) > n = historyLength-1 > # get the speeds from the previous n (hisoryLength) "Movements" > for (j in seq(1, length(historyLength))){ > speedHistory [n] = R[i-j, 6] > n-1 > } > > if (!bayesFilter(speedHistory, minSpeed, GPS_accy)) movement <- > "non-moving" > else if(bayesFilter(speedHistory, minSpeed, GPS_accy)) movement <- > movementType(relAngle, angleThreshold) > > > } > > > holder <- list(movement) > holder [[i]] <- (movement) > > > for (t in length(holder)){ > if (holder[[t]] == holder[[t-1]]) > changes <- 0 > else changes <- 1 > > } > > > > # update previous heading information > previousHeading = heading > > > > > > > # Store the input data and the results > lst[[i]] <- c( > latitude_position_1 = lat1, > longtude_position_1 = long1, > latitude_position_2 = lat2, > longtude_position_2 = long2, > Distance = gcd_vif, > Speed = estSpeed, > Acceleration = accel, > Absolute_Heading = heading, > Relative_Heading = relAngle, > Movement = movement, > Changes = changes > > ) > > } > Results <- as.data.frame(do.call(rbind, lst)) # store the input data and > the > results in a data frame > Results > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/for-loop-not-working-tp4713392.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] for loop not working
Hi. I have some code which loops through raw GPS data. This initial loop calculates distance, angle, speed etc between successive coordinates as well as type of movement e.g.left, right, forward. I want to construct a second loop that goes through the movements and records 'Changes' as either '0' or '1' depending on whether the movement changed or not. I have put the whole code in for reference but the additional loop begins at the object 'holder.' I want to store movements in holder and then loop through holder to determine 'changes.' At the moment it gives me 'Error in holder[[t - 1]] : attempt to select less than one element.' The moment i make holder [[t]] it works but just gives a list of '0's. I have tried many different things and just cannot get it to work, so any help would be very much appreciated. Again, sorry for the long post. lst <- list() # temporary list to store the results for (i in seq(1, nrow(R) - 1)){ # loop through each row of the 'R' matrix lat1 <- deg2rad(R[i, 4]) long1 <- deg2rad(R[i, 5]) lat2 <- deg2rad(R[i + 1, 4]) long2 <- deg2rad(R[i + 1, 5]) gcd_vif <- gcd.vif(lat1, long1, lat2, long2) # calc estimated speed by mean of speeds between two GPS records estSpeed <- (R[i, 6] + R[i+1, 6])/2 # calculate acceleration (velocity) accel <- (R[i+1, 6] - R[i, 6]) / GPSSample # calculate absolute heading heading <- absoluteHeading(lat1, long1, lat2, long2) # calculate bearing relative to previous GPS record relAngle <- 0 # if the number of GPS records is less than 3 then no change in track if (i < 1) relAngle = heading # otherwise consider the previous heading in order to calculate the new track else if (i > 0) { relAngle = (previousHeading - heading) } # determine whether movement is occurring and if so what type # if there are insufficient history items then just record movement types discretely if (i < historyLength) movement <- movementType(relAngle, angleThreshold) else if (i > historyLength-1) { # Array to store speeds speedHistory <- array(historyLength) n = historyLength-1 # get the speeds from the previous n (hisoryLength) "Movements" for (j in seq(1, length(historyLength))){ speedHistory [n] = R[i-j, 6] n-1 } if (!bayesFilter(speedHistory, minSpeed, GPS_accy)) movement <- "non-moving" else if(bayesFilter(speedHistory, minSpeed, GPS_accy)) movement <- movementType(relAngle, angleThreshold) } holder <- list(movement) holder [[i]] <- (movement) for (t in length(holder)){ if (holder[[t]] == holder[[t-1]]) changes <- 0 else changes <- 1 } # update previous heading information previousHeading = heading # Store the input data and the results lst[[i]] <- c( latitude_position_1 = lat1, longtude_position_1 = long1, latitude_position_2 = lat2, longtude_position_2 = long2, Distance = gcd_vif, Speed = estSpeed, Acceleration = accel, Absolute_Heading = heading, Relative_Heading = relAngle, Movement = movement, Changes = changes ) } Results <- as.data.frame(do.call(rbind, lst)) # store the input data and the results in a data frame Results -- View this message in context: http://r.789695.n4.nabble.com/for-loop-not-working-tp4713392.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] for loop not working
Dear Arun, thank-you very much. i m extremely sorry for spoiling your Christmas. eliza Date: Tue, 25 Dec 2012 08:51:05 -0800 From: smartpink...@yahoo.com Subject: Re: [R] for loop not working To: eliza_bo...@hotmail.com CC: r-help@r-project.org; kri...@ymail.com HI Eliza, Try this: set.seed(15) mat1-matrix(sample(1:2000,1776,replace=TRUE),ncol=444) colnames(mat1)-paste(Col,1:444,sep=) res1-lapply(1:37,function(i) mat1[,seq(i,444,37)]) res2-lapply(1:37,function(i) {a-mat1[,i:444];a[,c(TRUE,rep(FALSE,36))]}) #your code identical(res1,res2) #[1] TRUE From: eliza botto eliza_bo...@hotmail.com To: smartpink...@yahoo.com smartpink...@yahoo.com Cc: r-help@r-project.org r-help@r-project.org; kri...@ymail.com Sent: Tuesday, December 25, 2012 1:57 AM Subject: RE: [R] for loop not working Dear Arun, as usuall you were spot on. i tried the following lapply(seq_len(ncol(e)), function(i) { a-e[,(e[i]:444)] a[,c(TRUE, rep(FALSE,36))] }) but it never worked. thanks for your kind help. lots of love elisa Date: Mon, 24 Dec 2012 22:40:08 -0800 From: smartpink...@yahoo.com Subject: Re: [R] for loop not working To: eliza_bo...@hotmail.com CC: r-help@r-project.org; kri...@ymail.com HI Eliza, You could try this: set.seed(15) mat1-matrix(sample(1:2000,1776,replace=TRUE),ncol=444) colnames(mat1)-paste(Col,1:444,sep=) res-lapply(seq_len(ncol(mat1)),function(i) mat1[,seq(i,444,37)]) #If you want only this from 1:37, then res1-lapply(1:37,function(i) mat1[,seq(i,444,37)]) A.K. - Original Message - From: eliza botto eliza_bo...@hotmail.com To: r-help@r-project.org r-help@r-project.org Cc: Sent: Tuesday, December 25, 2012 12:03 AM Subject: [R] for loop not working dear R family,i have a matrix of 444 columns. what i want to do is the following. 1. starting from column 1 i want to select every 37th column on the way. more precisely i want to select column 1, 38,75,112,149 and so on. 2.starting from column 2, i again want to select every 37th column. which means 2,39,76,113,150 and so on. similarly starting from 3 till 37th column. i have tried following loop command which is not working.can anyone plz see whats wrong in that? for (i in 1:37) { a-e[,e[i]:444] } lapply(seq_len(1), function(i) { a[,c(TRUE, rep(FALSE,1))] }) extremly sorry for bothering you once again.. eliza [[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. [[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.
Re: [R] for loop not working
HI Eliza, Try this: set.seed(15) mat1-matrix(sample(1:2000,1776,replace=TRUE),ncol=444) colnames(mat1)-paste(Col,1:444,sep=) res1-lapply(1:37,function(i) mat1[,seq(i,444,37)]) res2-lapply(1:37,function(i) {a-mat1[,i:444];a[,c(TRUE,rep(FALSE,36))]}) #your code identical(res1,res2) #[1] TRUE From: eliza botto eliza_bo...@hotmail.com To: smartpink...@yahoo.com smartpink...@yahoo.com Cc: r-help@r-project.org r-help@r-project.org; kri...@ymail.com Sent: Tuesday, December 25, 2012 1:57 AM Subject: RE: [R] for loop not working Dear Arun, as usuall you were spot on. i tried the following lapply(seq_len(ncol(e)), function(i) { a-e[,(e[i]:444)] a[,c(TRUE, rep(FALSE,36))] }) but it never worked. thanks for your kind help. lots of love elisa Date: Mon, 24 Dec 2012 22:40:08 -0800 From: smartpink...@yahoo.com Subject: Re: [R] for loop not working To: eliza_bo...@hotmail.com CC: r-help@r-project.org; kri...@ymail.com HI Eliza, You could try this: set.seed(15) mat1-matrix(sample(1:2000,1776,replace=TRUE),ncol=444) colnames(mat1)-paste(Col,1:444,sep=) res-lapply(seq_len(ncol(mat1)),function(i) mat1[,seq(i,444,37)]) #If you want only this from 1:37, then res1-lapply(1:37,function(i) mat1[,seq(i,444,37)]) A.K. - Original Message - From: eliza botto eliza_bo...@hotmail.com To: r-help@r-project.org r-help@r-project.org Cc: Sent: Tuesday, December 25, 2012 12:03 AM Subject: [R] for loop not working dear R family,i have a matrix of 444 columns. what i want to do is the following. 1. starting from column 1 i want to select every 37th column on the way. more precisely i want to select column 1, 38,75,112,149 and so on. 2.starting from column 2, i again want to select every 37th column. which means 2,39,76,113,150 and so on. similarly starting from 3 till 37th column. i have tried following loop command which is not working.can anyone plz see whats wrong in that? for (i in 1:37) { a-e[,e[i]:444] } lapply(seq_len(1), function(i) { a[,c(TRUE, rep(FALSE,1))] }) extremly sorry for bothering you once again.. eliza [[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.
[R] for loop not working
dear R family,i have a matrix of 444 columns. what i want to do is the following. 1. starting from column 1 i want to select every 37th column on the way. more precisely i want to select column 1, 38,75,112,149 and so on. 2.starting from column 2, i again want to select every 37th column. which means 2,39,76,113,150 and so on. similarly starting from 3 till 37th column. i have tried following loop command which is not working.can anyone plz see whats wrong in that? for (i in 1:37) { a-e[,e[i]:444] } lapply(seq_len(1), function(i) { a[,c(TRUE, rep(FALSE,1))] }) extremly sorry for bothering you once again.. eliza [[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.
Re: [R] for loop not working
Hello, ?seq seq(i,444,37) with i = 1,2,3...37 HTH Pascal Le 25/12/2012 14:03, eliza botto a écrit : dear R family,i have a matrix of 444 columns. what i want to do is the following. 1. starting from column 1 i want to select every 37th column on the way. more precisely i want to select column 1, 38,75,112,149 and so on. 2.starting from column 2, i again want to select every 37th column. which means 2,39,76,113,150 and so on. similarly starting from 3 till 37th column. i have tried following loop command which is not working.can anyone plz see whats wrong in that? for (i in 1:37) { a-e[,e[i]:444] } lapply(seq_len(1), function(i) { a[,c(TRUE, rep(FALSE,1))] }) extremly sorry for bothering you once again.. eliza [[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] for loop not working
Dear Pascal,thanks for replying. it is working about it only executes the first set of column, it does not bring about all the sets of columns.any thoughts?? eliza Date: Tue, 25 Dec 2012 14:12:01 +0900 From: kri...@ymail.com To: eliza_bo...@hotmail.com CC: r-help@r-project.org Subject: Re: [R] for loop not working Hello, ?seq seq(i,444,37) with i = 1,2,3...37 HTH Pascal Le 25/12/2012 14:03, eliza botto a écrit : dear R family,i have a matrix of 444 columns. what i want to do is the following. 1. starting from column 1 i want to select every 37th column on the way. more precisely i want to select column 1, 38,75,112,149 and so on. 2.starting from column 2, i again want to select every 37th column. which means 2,39,76,113,150 and so on. similarly starting from 3 till 37th column. i have tried following loop command which is not working.can anyone plz see whats wrong in that? for (i in 1:37) { a-e[,e[i]:444] } lapply(seq_len(1), function(i) { a[,c(TRUE, rep(FALSE,1))] }) extremly sorry for bothering you once again.. eliza [[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. [[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.
Re: [R] for loop not working
Hello, I provided you all the information you need. Replace i by 1,2,3...37 in seq(i,444,37). Pascal Le 25/12/2012 14:19, eliza botto a écrit : Dear Pascal, thanks for replying. it is working about it only executes the first set of column, it does not bring about all the sets of columns. any thoughts?? eliza Date: Tue, 25 Dec 2012 14:12:01 +0900 From: kri...@ymail.com To: eliza_bo...@hotmail.com CC: r-help@r-project.org Subject: Re: [R] for loop not working Hello, ?seq seq(i,444,37) with i = 1,2,3...37 HTH Pascal Le 25/12/2012 14:03, eliza botto a écrit : dear R family,i have a matrix of 444 columns. what i want to do is the following. 1. starting from column 1 i want to select every 37th column on the way. more precisely i want to select column 1, 38,75,112,149 and so on. 2.starting from column 2, i again want to select every 37th column. which means 2,39,76,113,150 and so on. similarly starting from 3 till 37th column. i have tried following loop command which is not working.can anyone plz see whats wrong in that? for (i in 1:37) { a-e[,e[i]:444] } lapply(seq_len(1), function(i) { a[,c(TRUE, rep(FALSE,1))] }) extremly sorry for bothering you once again.. eliza [[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] for loop not working
Dear Arun,as usuall you were spot on. i tried the following lapply(seq_len(ncol(e)), function(i) { a-e[,(e[i]:444)] a[,c(TRUE, rep(FALSE,36))] }) but it never worked. thanks for your kind help.lots of love elisa Date: Mon, 24 Dec 2012 22:40:08 -0800 From: smartpink...@yahoo.com Subject: Re: [R] for loop not working To: eliza_bo...@hotmail.com CC: r-help@r-project.org; kri...@ymail.com HI Eliza, You could try this: set.seed(15) mat1-matrix(sample(1:2000,1776,replace=TRUE),ncol=444) colnames(mat1)-paste(Col,1:444,sep=) res-lapply(seq_len(ncol(mat1)),function(i) mat1[,seq(i,444,37)]) #If you want only this from 1:37, then res1-lapply(1:37,function(i) mat1[,seq(i,444,37)]) A.K. - Original Message - From: eliza botto eliza_bo...@hotmail.com To: r-help@r-project.org r-help@r-project.org Cc: Sent: Tuesday, December 25, 2012 12:03 AM Subject: [R] for loop not working dear R family,i have a matrix of 444 columns. what i want to do is the following. 1. starting from column 1 i want to select every 37th column on the way. more precisely i want to select column 1, 38,75,112,149 and so on. 2.starting from column 2, i again want to select every 37th column. which means 2,39,76,113,150 and so on. similarly starting from 3 till 37th column. i have tried following loop command which is not working.can anyone plz see whats wrong in that? for (i in 1:37) { a-e[,e[i]:444] } lapply(seq_len(1), function(i) { a[,c(TRUE, rep(FALSE,1))] }) extremly sorry for bothering you once again.. eliza [[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. [[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.
Re: [R] for loop not working
Hi, Why don't you use Arun's solution? Regards, Pascal Le 25/12/2012 15:57, eliza botto a écrit : Dear Arun, as usuall you were spot on. i tried the following *_lapply(_**_seq_len(ncol(e)), function(i) {_* _ _ _a-e[,(e[i]:444)]_ a[,c(TRUE, rep(FALSE,36))] }) but it never worked. thanks for your kind help. lots of love elisa Date: Mon, 24 Dec 2012 22:40:08 -0800 From: smartpink...@yahoo.com Subject: Re: [R] for loop not working To: eliza_bo...@hotmail.com CC: r-help@r-project.org; kri...@ymail.com HI Eliza, You could try this: set.seed(15) mat1-matrix(sample(1:2000,1776,replace=TRUE),ncol=444) colnames(mat1)-paste(Col,1:444,sep=) res-lapply(seq_len(ncol(mat1)),function(i) mat1[,seq(i,444,37)]) #If you want only this from 1:37, then res1-lapply(1:37,function(i) mat1[,seq(i,444,37)]) A.K. - Original Message - From: eliza botto eliza_bo...@hotmail.com To: r-help@r-project.org r-help@r-project.org Cc: Sent: Tuesday, December 25, 2012 12:03 AM Subject: [R] for loop not working dear R family,i have a matrix of 444 columns. what i want to do is the following. 1. starting from column 1 i want to select every 37th column on the way. more precisely i want to select column 1, 38,75,112,149 and so on. 2.starting from column 2, i again want to select every 37th column. which means 2,39,76,113,150 and so on. similarly starting from 3 till 37th column. i have tried following loop command which is not working.can anyone plz see whats wrong in that? for (i in 1:37) { a-e[,e[i]:444] } lapply(seq_len(1), function(i) { a[,c(TRUE, rep(FALSE,1))] }) extremly sorry for bothering you once again.. eliza [[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] for loop not working
HI Eliza, You could try this: set.seed(15) mat1-matrix(sample(1:2000,1776,replace=TRUE),ncol=444) colnames(mat1)-paste(Col,1:444,sep=) res-lapply(seq_len(ncol(mat1)),function(i) mat1[,seq(i,444,37)]) #If you want only this from 1:37, then res1-lapply(1:37,function(i) mat1[,seq(i,444,37)]) A.K. - Original Message - From: eliza botto eliza_bo...@hotmail.com To: r-help@r-project.org r-help@r-project.org Cc: Sent: Tuesday, December 25, 2012 12:03 AM Subject: [R] for loop not working dear R family,i have a matrix of 444 columns. what i want to do is the following. 1. starting from column 1 i want to select every 37th column on the way. more precisely i want to select column 1, 38,75,112,149 and so on. 2.starting from column 2, i again want to select every 37th column. which means 2,39,76,113,150 and so on. similarly starting from 3 till 37th column. i have tried following loop command which is not working.can anyone plz see whats wrong in that? for (i in 1:37) { a-e[,e[i]:444] } lapply(seq_len(1), function(i) { a[,c(TRUE, rep(FALSE,1))] }) extremly sorry for bothering you once again.. eliza [[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.
[R] For loop isn't working correctly
Hi. With your help, I've fixed the errors in my for loops. But now, the for loop isn't working correctly. There should be a plot, but there's no plot when I run the for loop.. This is the code: letters = c(A,B,C,D,E,F,G,H,I,J) numbers = 1:3 for(i in 1:6){ #6 letters for (j in 1:3) { #3 numbers for (k in -1:1) { #answer -1,right or +1 fn = paste (i,j,k,.bmp,sep=) bmp(file = fn) plot(x = 10, y = 10, ylim=c(0,1), xlim=c(0,1), axes=FALSE, ylab=, xlab=) text(0.5,0.5, labels = paste(letters[i], +, numbers[j],=, letters [i+j+k])) dev.off() } } } Can somebody see why this isn't running correctly? Thanks for the help untill now! Greetings _ [[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.
Re: [R] For loop isn't working correctly
It plots on my system just fine. You might want to check what directory (getwd()) that you are plotting in. On Fri, Oct 9, 2009 at 12:17 PM, Anne Buunk ann3bu...@hotmail.com wrote: Hi. With your help, I've fixed the errors in my for loops. But now, the for loop isn't working correctly. There should be a plot, but there's no plot when I run the for loop.. This is the code: letters = c(A,B,C,D,E,F,G,H,I,J) numbers = 1:3 for(i in 1:6){ #6 letters for (j in 1:3) { #3 numbers for (k in -1:1) { #answer -1,right or +1 fn = paste (i,j,k,.bmp,sep=) bmp(file = fn) plot(x = 10, y = 10, ylim=c(0,1), xlim=c(0,1), axes=FALSE, ylab=, xlab=) text(0.5,0.5, labels = paste(letters[i], +, numbers[j],=, letters [i+j+k])) dev.off() } } } Can somebody see why this isn't running correctly? Thanks for the help untill now! Greetings _ [[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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that 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.