Dear R-group, is there a way to perform calculations that are defined in a string format? for example I have different variables: x1 <- 3 x2 <- 1 x4 <- 1 and a string-variable: do <- 'x1 + x2 + x3' Is there any way to perform what the variable 'do'-describes (just like the formula-element but more elemental)? Perhaps my idea to solve my problem is a little bit strange. My general problem is, that I have to do arithmetics for which there seems to be no function available that I can apply in order to be more flexible. To be precise, I have to add up three dimensional arrays. I can do that like this (as someone suggested on this help-list ? thanks for that!): (array[,,1] + array[,,2] + array[,,3]) / 3 However in my case it can happen that at some point, I don't have to add 3 but 8 'array-slices' (or 10 or x). And I don't want to manually expand the above statement to: (array[,,1] + array[,,2] + array[,,3] + array[,,4] + array[,,5] + array[,,6] + array[,,7] + array[,,8]) / 8 (ok, now I have done it ;) So, my thinking was that I can easily expand and change a string (with the paste-function / repeat-function etc.). But how can I expand a mathematical statement? thanks for any suggestions!
do calculations as defined by a string / expand mathematical statements in R
9 messages · William Dunlap, Jean-Christophe BOUËTTÉ, R. Michael Weylandt +2 more
Avoid parsing strings to make expressions. It is easy
to do, but hard to do safely and readably.
In your case you could make a short loop out of it
result <- x[,,,1]
for(i in seq_len(dim(x)[4])[-1]) {
result <- result + x[,,,i]
}
result <- result / dim(x)[4]
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Martin Batholdy Sent: Wednesday, October 05, 2011 1:14 PM To: R Help Subject: [R] do calculations as defined by a string / expand mathematical statements in R Dear R-group, is there a way to perform calculations that are defined in a string format? for example I have different variables: x1 <- 3 x2 <- 1 x4 <- 1 and a string-variable: do <- 'x1 + x2 + x3' Is there any way to perform what the variable 'do'-describes (just like the formula-element but more elemental)? Perhaps my idea to solve my problem is a little bit strange. My general problem is, that I have to do arithmetics for which there seems to be no function available that I can apply in order to be more flexible. To be precise, I have to add up three dimensional arrays. I can do that like this (as someone suggested on this help-list - thanks for that!): (array[,,1] + array[,,2] + array[,,3]) / 3 However in my case it can happen that at some point, I don't have to add 3 but 8 'array-slices' (or 10 or x). And I don't want to manually expand the above statement to: (array[,,1] + array[,,2] + array[,,3] + array[,,4] + array[,,5] + array[,,6] + array[,,7] + array[,,8]) / 8 (ok, now I have done it ;) So, my thinking was that I can easily expand and change a string (with the paste-function / repeat- function etc.). But how can I expand a mathematical statement? thanks for any suggestions!
______________________________________________ R-help at 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.
Didn't three of us give you a function (in various flavors) that would do the mean for variable inputs, reading them from a list? (Though David's was admittedly much cooler than mine!) Anyways, look into parse(text=do) with eval() if you want to go the string route. Michael
On Wed, Oct 5, 2011 at 4:14 PM, Martin Batholdy <batholdy at googlemail.com> wrote:
Dear R-group, is there a way to perform calculations that are defined in a string format? for example I have different variables: x1 <- 3 x2 <- 1 x4 <- 1 and a string-variable: do <- 'x1 + x2 + x3' Is there any way to perform what the variable 'do'-describes (just like the formula-element but more elemental)? Perhaps my idea to solve my problem is a little bit strange. My general problem is, that I have to do arithmetics for which there seems to be no function available that I can apply in order to be more flexible. To be precise, I have to add up three dimensional arrays. I can do that like this (as someone suggested on this help-list ? thanks for that!): (array[,,1] + array[,,2] + array[,,3]) / 3 However in my case it can happen that at some point, I don't have to add 3 but 8 'array-slices' (or 10 or x). And I don't want to manually expand the above statement to: (array[,,1] + array[,,2] + array[,,3] + array[,,4] + array[,,5] + array[,,6] + array[,,7] + array[,,8]) / 8 (ok, now I have done it ;) So, my thinking was that I can easily expand and change a string (with the paste-function / repeat-function etc.). But how can I expand a mathematical statement? thanks for any suggestions!
______________________________________________ R-help at 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.
Hi, are you looking for # reproducible example x <- 1:1000 dim(x)<-rep(10,3) # code apply(x,1:2,sum) note that ?apply works with many functions... 2011/10/5 Martin Batholdy <batholdy at googlemail.com>:
Dear R-group, is there a way to perform calculations that are defined in a string format? for example I have different variables: x1 <- 3 x2 <- 1 x4 <- 1 and a string-variable: do <- 'x1 + x2 + x3' Is there any way to perform what the variable 'do'-describes (just like the formula-element but more elemental)? Perhaps my idea to solve my problem is a little bit strange. My general problem is, that I have to do arithmetics for which there seems to be no function available that I can apply in order to be more flexible. To be precise, I have to add up three dimensional arrays. I can do that like this (as someone suggested on this help-list ? thanks for that!): (array[,,1] + array[,,2] + array[,,3]) / 3 However in my case it can happen that at some point, I don't have to add 3 but 8 'array-slices' (or 10 or x). And I don't want to manually expand the above statement to: (array[,,1] + array[,,2] + array[,,3] + array[,,4] + array[,,5] + array[,,6] + array[,,7] + array[,,8]) / 8 (ok, now I have done it ;) So, my thinking was that I can easily expand and change a string (with the paste-function / repeat-function etc.). But how can I expand a mathematical statement? thanks for any suggestions!
______________________________________________ R-help at 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.
Actually, this may just be a typo in your first post, but if you actually want to do this calculation: (array[,,1] + array[,,2] + array[,,3] + array[,,4] + array[,,5] + array[,,6] + array[,,7] + array[,,8]) / 8 Wouldn't this work? apply(array,3,sum)/dim(array)[3] On Wed, Oct 5, 2011 at 4:22 PM, R. Michael Weylandt
<michael.weylandt at gmail.com> wrote:
Didn't three of us give you a function (in various flavors) that would do the mean for variable inputs, reading them from a list? (Though David's was admittedly much cooler than mine!) Anyways, look into parse(text=do) with eval() if you want to go the string route. Michael On Wed, Oct 5, 2011 at 4:14 PM, Martin Batholdy <batholdy at googlemail.com> wrote:
Dear R-group, is there a way to perform calculations that are defined in a string format? for example I have different variables: x1 <- 3 x2 <- 1 x4 <- 1 and a string-variable: do <- 'x1 + x2 + x3' Is there any way to perform what the variable 'do'-describes (just like the formula-element but more elemental)? Perhaps my idea to solve my problem is a little bit strange. My general problem is, that I have to do arithmetics for which there seems to be no function available that I can apply in order to be more flexible. To be precise, I have to add up three dimensional arrays. I can do that like this (as someone suggested on this help-list ? thanks for that!): (array[,,1] + array[,,2] + array[,,3]) / 3 However in my case it can happen that at some point, I don't have to add 3 but 8 'array-slices' (or 10 or x). And I don't want to manually expand the above statement to: (array[,,1] + array[,,2] + array[,,3] + array[,,4] + array[,,5] + array[,,6] + array[,,7] + array[,,8]) / 8 (ok, now I have done it ;) So, my thinking was that I can easily expand and change a string (with the paste-function / repeat-function etc.). But how can I expand a mathematical statement? thanks for any suggestions!
______________________________________________ R-help at 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.
Sorry!! meant: apply(array,1:2,sum)/dim(array)[3] M On Wed, Oct 5, 2011 at 4:31 PM, R. Michael Weylandt
<michael.weylandt at gmail.com> wrote:
Actually, this may just be a typo in your first post, but if you actually want to do this calculation: (array[,,1] + array[,,2] + array[,,3] + array[,,4] + array[,,5] + array[,,6] + array[,,7] + array[,,8]) / 8 Wouldn't this work? apply(array,3,sum)/dim(array)[3] On Wed, Oct 5, 2011 at 4:22 PM, R. Michael Weylandt <michael.weylandt at gmail.com> wrote:
Didn't three of us give you a function (in various flavors) that would do the mean for variable inputs, reading them from a list? (Though David's was admittedly much cooler than mine!) Anyways, look into parse(text=do) with eval() if you want to go the string route. Michael On Wed, Oct 5, 2011 at 4:14 PM, Martin Batholdy <batholdy at googlemail.com> wrote:
Dear R-group, is there a way to perform calculations that are defined in a string format? for example I have different variables: x1 <- 3 x2 <- 1 x4 <- 1 and a string-variable: do <- 'x1 + x2 + x3' Is there any way to perform what the variable 'do'-describes (just like the formula-element but more elemental)? Perhaps my idea to solve my problem is a little bit strange. My general problem is, that I have to do arithmetics for which there seems to be no function available that I can apply in order to be more flexible. To be precise, I have to add up three dimensional arrays. I can do that like this (as someone suggested on this help-list ? thanks for that!): (array[,,1] + array[,,2] + array[,,3]) / 3 However in my case it can happen that at some point, I don't have to add 3 but 8 'array-slices' (or 10 or x). And I don't want to manually expand the above statement to: (array[,,1] + array[,,2] + array[,,3] + array[,,4] + array[,,5] + array[,,6] + array[,,7] + array[,,8]) / 8 (ok, now I have done it ;) So, my thinking was that I can easily expand and change a string (with the paste-function / repeat-function etc.). But how can I expand a mathematical statement? thanks for any suggestions!
______________________________________________ R-help at 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111005/4de5b863/attachment.pl>
# Changing to variable Z since array() is a function apply(Z.temp <- Z[,,,a:b],1:3,sum)/dim(Z.temp)[4] # Should work, though it may be more clear to define Z.temp in its own line M
On Wed, Oct 5, 2011 at 5:10 PM, Martin Batholdy <batholdy at googlemail.com> wrote:
Thanks for all the suggestions! Perhaps my post was not clear enough. apply(array,1:2,sum)/dim(array)[3] and # reproducible example x <- 1:1000 dim(x)<-rep(10,3) # code apply(x,1:2,sum) would give me the mean over one whole dimension, right? The problem with that is, that I just want to calculate the mean over a subset of t (where t is the 4th dimension of the array). And the range of this subset should be easily changeable. So for example I have 4D array: x <- 1:10000 dim(x)<-rep(10,4) Now I would like to average the 3D array(x,y,z) in the 4th dimension (t) from t_start = a to t_end = b. I don't want to average the whole 3D array. On 05.10.2011, at 22:21, William Dunlap wrote:
Avoid parsing strings to make expressions. ?It is easy
to do, but hard to do safely and readably.
In your case you could make a short loop out of it
? result <- x[,,,1]
? for(i in seq_len(dim(x)[4])[-1]) {
? ? ?result <- result + x[,,,i]
? }
? result <- result / dim(x)[4]
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
Wouldn't that be much slower than define a string and evaluate it as an expression since I would have to use a for-loop? thanks again! You helped me a lot today ;) On 05.10.2011, at 22:21, William Dunlap wrote:
Avoid parsing strings to make expressions. ?It is easy
to do, but hard to do safely and readably.
In your case you could make a short loop out of it
? result <- x[,,,1]
? for(i in seq_len(dim(x)[4])[-1]) {
? ? ?result <- result + x[,,,i]
? }
? result <- result / dim(x)[4]
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Martin Batholdy Sent: Wednesday, October 05, 2011 1:14 PM To: R Help Subject: [R] do calculations as defined by a string / expand mathematical statements in R Dear R-group, is there a way to perform calculations that are defined in a string format? for example I have different variables: x1 <- 3 x2 <- 1 x4 <- 1 and a string-variable: do <- 'x1 + x2 + x3' Is there any way to perform what the variable 'do'-describes (just like the formula-element but more elemental)? Perhaps my idea to solve my problem is a little bit strange. My general problem is, that I have to do arithmetics for which there seems to be no function available that I can apply in order to be more flexible. To be precise, I have to add up three dimensional arrays. I can do that like this (as someone suggested on this help-list - thanks for that!): (array[,,1] + array[,,2] + array[,,3]) / 3 However in my case it can happen that at some point, I don't have to add 3 but 8 'array-slices' (or 10 or x). And I don't want to manually expand the above statement to: (array[,,1] + array[,,2] + array[,,3] + array[,,4] + array[,,5] + array[,,6] + array[,,7] + array[,,8]) / 8 (ok, now I have done it ;) So, my thinking was that I can easily expand and change a string (with the paste-function / repeat- function etc.). But how can I expand a mathematical statement? thanks for any suggestions!
______________________________________________ R-help at 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 at 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.
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Martin Batholdy Sent: Wednesday, October 05, 2011 2:10 PM To: R Help Subject: Re: [R] do calculations as defined by a string / expandmathematical statements in R Thanks for all the suggestions! Perhaps my post was not clear enough. apply(array,1:2,sum)/dim(array)[3] and # reproducible example x <- 1:1000 dim(x)<-rep(10,3) # code apply(x,1:2,sum) would give me the mean over one whole dimension, right? The problem with that is, that I just want to calculate the mean over a subset of t (where t is the 4th dimension of the array). And the range of this subset should be easily changeable. So for example I have 4D array: x <- 1:10000 dim(x)<-rep(10,4) Now I would like to average the 3D array(x,y,z) in the 4th dimension (t) from t_start = a to t_end = b. I don't want to average the whole 3D array.
I don't have specific advice to address your question, but I do have an observation on this series of posts. It is often the case that people new to R try to program like they would if they were using C, or SAS, or ... whatever they are used to. I can't help but think that if you provided some context for what your tasks are you might find that someone on the list is working in the same area and could provide advice tailored to what you need to do. Why, there might even be a package or two that already provide the functionality that you need. So, broadly speaking, what do these multidimensional arrays represent and what are you trying to do with them? Dan Daniel Nordlund Bothell, WA USA