Re: [R] using a package function inside another function

2010-10-07 Thread Ethan Brown
Hi Alison,

By default, a function in R creates a copy of the variable that you
pass into it. insertRow() looks to be unusual in that it actually
changes the variable you pass into the function.

So if you run your insert_row_test(x), the function will create a copy
of x, insert a row into it using insertRow(), and then return that new
matrix.  So all you need to do is assign the output of your function
to the original object, like so:

> x <- matrix(1:9, 3, 3)

> x
 [,1] [,2] [,3]
[1,]147
[2,]258
[3,]369

> x <- insert_row_test(x)

> x
 [,1] [,2] [,3]
147
258
369
test000


See 
http://cran.r-project.org/doc/manuals/R-intro.html#Writing-your-own-functions
for more on this topic.

HTH,
Ethan Brown


On Thu, Oct 7, 2010 at 9:47 AM, Alison Callahan
 wrote:
> Hello all,
>
> I am trying to use the micEcon 'insertRow' function inside a function
> I have written. For example:
>
> insert_row_test <- function(m){
>
>  insertRow(m,nrow(m)+1,v=0,rName="test")
>
> }
>
> However, when I try to call the 'insert_row_test' function (after
> loading the micEcon package), it does not insert a row into the matrix
> I pass in. When I call the insertRow function exactly as above in the
> R console, it works with no problem.
>
> Can anyone tell me why this is, and how to fix this problem?
>
> Thank you!
>
> Alison Callahan
> PhD candidate
> Department of Biology
> Carleton University
>
> __
> 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] using a package function inside another function

2010-10-07 Thread Andrew Miles

Try adding a statement at the beginning of your function:

require(micEcon)

See if that helps.

Andrew Miles
Department of Sociology
Duke University

On Oct 7, 2010, at 11:47 AM, Alison Callahan wrote:


Hello all,

I am trying to use the micEcon 'insertRow' function inside a function
I have written. For example:

insert_row_test <- function(m){

 insertRow(m,nrow(m)+1,v=0,rName="test")

}

However, when I try to call the 'insert_row_test' function (after
loading the micEcon package), it does not insert a row into the matrix
I pass in. When I call the insertRow function exactly as above in the
R console, it works with no problem.

Can anyone tell me why this is, and how to fix this problem?

Thank you!

Alison Callahan
PhD candidate
Department of Biology
Carleton University

__
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] using a package function inside another function

2010-10-07 Thread Alison Callahan
Hello all,

I am trying to use the micEcon 'insertRow' function inside a function
I have written. For example:

insert_row_test <- function(m){

  insertRow(m,nrow(m)+1,v=0,rName="test")

}

However, when I try to call the 'insert_row_test' function (after
loading the micEcon package), it does not insert a row into the matrix
I pass in. When I call the insertRow function exactly as above in the
R console, it works with no problem.

Can anyone tell me why this is, and how to fix this problem?

Thank you!

Alison Callahan
PhD candidate
Department of Biology
Carleton University

__
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.