[R] apply question
Dear All, please help with the following problem: I have t -seq(0,24,by=6) a -600 g -0.05 b -a*exp(-g*t) I would like to establish a vector called z (for example) based on b where the results are calculated as : z -c(a-b[1],b[1]-b[2],b[2]-b[3],b[3]-b[4],b[4]-b[5]) so the results are: [1] 0.0 155.50907 115.20395 85.34519 63.22527 as always your input is appreciated Andras [[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] apply question
Homework? A hint is: ?diff Pat On 27/10/2013 11:37, Andras Farkas wrote: Dear All, please help with the following problem: I have t -seq(0,24,by=6) a -600 g -0.05 b -a*exp(-g*t) I would like to establish a vector called z (for example) based on b where the results are calculated as : z -c(a-b[1],b[1]-b[2],b[2]-b[3],b[3]-b[4],b[4]-b[5]) so the results are: [1] 0.0 155.50907 115.20395 85.34519 63.22527 as always your input is appreciated Andras [[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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ 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] apply question
not homework, but thanks for the hint! Andras On Sunday, October 27, 2013 7:51 AM, Patrick Burns pbu...@pburns.seanet.com wrote: Homework? A hint is: ?diff Pat On 27/10/2013 11:37, Andras Farkas wrote: Dear All, please help with the following problem: I have t -seq(0,24,by=6) a -600 g -0.05 b -a*exp(-g*t) I would like to establish a vector called z (for example) based on b where the results are calculated as : z -c(a-b[1],b[1]-b[2],b[2]-b[3],b[3]-b[4],b[4]-b[5]) so the results are: [1] 0.0 155.50907 115.20395 85.34519 63.22527 as always your input is appreciated Andras [[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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') [[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] apply question
This works, where zz is a dataframe: for(i in 1:nrow(zz)) { zzz[i,1]-paste(zz[i,1],zz[i,2],sep=_) } I would like to use apply to concatentate two columns of text along with a separator. How? Chet [[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] apply question
What is wrong with utilizing the vectorized nature of paste? zz[[1]] - paste(zz[[1]],zz[[2]],sep=_) --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Chet Seligman chet.selig...@gmail.com wrote: This works, where zz is a dataframe: for(i in 1:nrow(zz)) { zzz[i,1]-paste(zz[i,1],zz[i,2],sep=_) } I would like to use apply to concatentate two columns of text along with a separator. How? Chet [[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] apply question
You don't need loops or apply with paste since it's vectorized: x - data.frame(n = 1:5, l = letters[1:5], stringsAsFactors = FALSE) paste(x[,1], x[,2], sep = _) Cheers, Michael On Tue, Aug 21, 2012 at 10:57 AM, Chet Seligman chet.selig...@gmail.com wrote: This works, where zz is a dataframe: for(i in 1:nrow(zz)) { zzz[i,1]-paste(zz[i,1],zz[i,2],sep=_) } I would like to use apply to concatentate two columns of text along with a separator. How? Chet [[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] apply question
... or using the handy with() with(x,paste(n,l,sep=_)) Slightly greater clarity and robustness, perhaps... -- Bert On Tue, Aug 21, 2012 at 9:24 AM, R. Michael Weylandt michael.weyla...@gmail.com wrote: You don't need loops or apply with paste since it's vectorized: x - data.frame(n = 1:5, l = letters[1:5], stringsAsFactors = FALSE) paste(x[,1], x[,2], sep = _) Cheers, Michael On Tue, Aug 21, 2012 at 10:57 AM, Chet Seligman chet.selig...@gmail.com wrote: This works, where zz is a dataframe: for(i in 1:nrow(zz)) { zzz[i,1]-paste(zz[i,1],zz[i,2],sep=_) } I would like to use apply to concatentate two columns of text along with a separator. How? Chet [[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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.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.
[R] apply question
Why does one method work, and the other not? attach(iris) head(iris) mean(Sepal.Length) [1] 5.84 apply(iris, Sepal.Length, mean) Error in if (d2 == 0L) { : missing value where TRUE/FALSE needed I seem to get this error frequently with the apply functions but other manual methods work normally on the same data. TIA -- View this message in context: http://r.789695.n4.nabble.com/apply-question-tp3821122p3821122.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] apply question
Hi clsnyder, You forgot the MARGIN argument in you function call. Take a look at ?apply apply(iris, 2, length) apply(iris, 2, mean) HTH, Jorge * * On Sat, Sep 17, 2011 at 6:42 PM, clsnyder wrote: Why does one method work, and the other not? attach(iris) head(iris) mean(Sepal.Length) [1] 5.84 apply(iris, Sepal.Length, mean) Error in if (d2 == 0L) { : missing value where TRUE/FALSE needed I seem to get this error frequently with the apply functions but other manual methods work normally on the same data. TIA -- View this message in context: http://r.789695.n4.nabble.com/apply-question-tp3821122p3821122.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. [[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] apply question
Hi r-help-boun...@r-project.org napsal dne 30.04.2010 23:11:54: Hello David, On Apr 30, 2010, at 11:00 PM, David Winsemius wrote: Note: Loops may be just as fast or faster than apply calls. How come!? is this true also for other similar functions: lapply, tapply and sapply? Then the only advantage of these above is only syntactic sugar? Yes and no. If your loop(s) is poorly written it can be much slower then *apply operator but usually not from loop point of view but from the code itself. Regards Petr indices - replicate(B,sample(1:N, size=N, replace=TRUE)) theta_star - apply(indices,2,statistic,data) Why not: theta_star - apply(indices,2, function(x) statistic(data, x) ) May end up as a data class that needs further work, depending on what statistic return. Nice! thank you! Best regards, Giovanni __ 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] apply question
Hello, I have a bootstrap implementation loop that I would like to replace by a faster batch operator. The loop is as follows: for (b in 1:B) { indices - sample(1:N, size=N, replace=TRUE) # sample n elements with replacement theta_star[b,] = statistic(data,indices) # execute statistic callback function } I would like to replace this by something like (does not work): indices - replicate(B,sample(1:N, size=N, replace=TRUE)) theta_star - apply(indices,2,statistic,data) This, however, does not work because the statistic function receives indices as second parameter and not as first parameter. While I could change the signature of my statistic function I would like to know how to achieve this without having to do so. TIA, Best regards, Giovanni [[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] apply question
On Apr 30, 2010, at 4:52 PM, Giovanni Azua wrote: Hello, I have a bootstrap implementation loop that I would like to replace by a faster batch operator. The loop is as follows: for (b in 1:B) { indices - sample(1:N, size=N, replace=TRUE) # sample n elements with replacement theta_star[b,] = statistic(data,indices) # execute statistic callback function } I would like to replace this by something like (does not work): Note: Loops may be just as fast or faster than apply calls. indices - replicate(B,sample(1:N, size=N, replace=TRUE)) theta_star - apply(indices,2,statistic,data) Why not: theta_star - apply(indices,2, function(x) statistic(data, x) ) May end up as a data class that needs further work, depending on what statistic return. This, however, does not work because the statistic function receives indices as second parameter and not as first parameter. While I could change the signature of my statistic function I would like to know how to achieve this without having to do so. TIA, Best regards, Giovanni [[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. David Winsemius, MD 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] apply question
Hello David, On Apr 30, 2010, at 11:00 PM, David Winsemius wrote: Note: Loops may be just as fast or faster than apply calls. How come!? is this true also for other similar functions: lapply, tapply and sapply? Then the only advantage of these above is only syntactic sugar? indices - replicate(B,sample(1:N, size=N, replace=TRUE)) theta_star - apply(indices,2,statistic,data) Why not: theta_star - apply(indices,2, function(x) statistic(data, x) ) May end up as a data class that needs further work, depending on what statistic return. Nice! thank you! Best regards, Giovanni __ 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.