Confusing piece of R code

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Confusing piece of R code

Bazman76
m0<-epxression((4*theta1*theta2-theta3^2)/(2*x*theta3^2)-0.5*theta1*x)

params<-all.vars(m0)                              this reads all the params from m0 so theta1,2 and 3 correct?
params<-params[-which(params=="x")]      checks which params are multiplied by x?
np<-length(params)                                

for(i in 1:6){
esp<-get(sprintf("m%d",i-1))                    what does get do? sprinf formats strings? so what is it doinf here?
assign(sprintf("m%d",i),D(esp,"x"))             what doeas assign so what in sprintf doing and what does D do?
}

really really confused?
Reply | Threaded
Open this post in threaded view
|

Re: Confusing piece of R code

Brad Schneid
Have you looked at the manual for any of these?

?get
?sprintf
?assign
#and so on...




?
Bazman76 wrote
m0<-epxression((4*theta1*theta2-theta3^2)/(2*x*theta3^2)-0.5*theta1*x)

params<-all.vars(m0)                              this reads all the params from m0 so theta1,2 and 3 correct?
params<-params[-which(params=="x")]      checks which params are multiplied by x?
np<-length(params)                                

for(i in 1:6){
esp<-get(sprintf("m%d",i-1))                    what does get do? sprinf formats strings? so what is it doinf here?
assign(sprintf("m%d",i),D(esp,"x"))             what doeas assign so what in sprintf doing and what does D do?
}

really really confused?
Reply | Threaded
Open this post in threaded view
|

Re: Confusing piece of R code

Sarah Goslee
In reply to this post by Bazman76
The neat thing about R is that it's interpreted, so you can look for yourself.

On Sat, Jul 9, 2011 at 3:46 PM, Bazman76 <[hidden email]> wrote:
> m0<-epxression((4*theta1*theta2-theta3^2)/(2*x*theta3^2)-0.5*theta1*x)
>
> params<-all.vars(m0)                              this reads all the params
> from m0 so theta1,2 and 3 correct?

> m0<-expression((4*theta1*theta2-theta3^2)/(2*x*theta3^2)-0.5*theta1*x)
> params<-all.vars(m0)
> params
[1] "theta1" "theta2" "theta3" "x"

So no, not just the thetas but also x


> params<-params[-which(params=="x")]      checks which params are multiplied
> by x?

> params<-params[-which(params=="x")]
> params
[1] "theta1" "theta2" "theta3"

Nope, see that it takes out the params that are not equal to x. You
could look at, for instance ?which to figure that out, and also ?"=="

> np<-length(params)
>
> for(i in 1:6){
> esp<-get(sprintf("m%d",i-1))                    what does get do? sprinf
> formats strings? so what is it doinf here?

Again, you can try it.

> i <- 1
> sprintf("m%d",i-1)
[1] "m0"

So it's taking the value of i, and combining it with some other things
to make a character name. get() will get the R object with that name
and assign it to esp
?get will tell you that.

> assign(sprintf("m%d",i),D(esp,"x"))             what doeas assign so what in
> sprintf doing and what does D do?
> }

Same thing in reverse. I'll leave this one as an exercise for the querent.

Sarah

> really really confused?

Learning to try things and to read the help are the only cure.

Sarah

--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Confusing piece of R code

Bazman76
thanks sarah