Re: [R] write data from function into external table

2006-06-14 Thread Gabor Grothendieck
If functions don't modify their environment then its generally
easier to debug programs so it encourarges better programming.

On 6/14/06, Sebastian Leuzinger <[EMAIL PROTECTED]> wrote:
> Thanks for the hints, they are useful to me. However, I still do not
> understand why my approach fails. In a user defined function, R does not seem
> to want to write into an object defined outside this function. (see my first
> example below). I guess there is some logic behind this.
>
> On Wednesday 14 June 2006 14:22, you wrote:
> > Here are two alternatives.  See ?"<<-"
> >
> > testfct1 <- function() test[1] <<- 100
> >
> > # following one can be written more compactly as
> > #   testfct2 <- function(test) replace(test, 1, 100)
> > testfct2 <- function(test) { test[1] <- 100; test }
> >
> > # test
> > test <- 1:3; testfct1(); test
> > test <- 1:3; test <- testfct2(test); test
> >
> > On 6/14/06, Sebastian Leuzinger <[EMAIL PROTECTED]> wrote:
> > > Dear list,
> > > My apologies if a solution / explanation to this already exists on the
> > > list, but it is difficult to assign it to a certain keyword.
> > >
> > > test<-c(1:3)
> > > testfct <- function(x) {test[1]<-100}
> > >  test
> > > [1] 1 2 3
> > >  testfct(1)
> > > [1] 1 2 3
> > >
> > > Basically, I would like to write data into an external table that the
> > > function does not know. Why is this not working / what alternatives
> > > exist?
> > >
> > > Thanks, Sebastian
> > >
> > > 
> > > Sebastian Leuzinger
> > > University of Basel, Department of Environmental Science
> > > Institute of Botany
> > > Schönbeinstr. 6 CH-4056 Basel
> > > ph0041 (0) 61 2673511
> > > fax   0041 (0) 61 2673504
> > > email [EMAIL PROTECTED]
> > > web   http://pages.unibas.ch/botschoen/leuzinger
> > >
> > > __
> > > R-help@stat.math.ethz.ch mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide!
> > > http://www.R-project.org/posting-guide.html
>
> --
> 
> Sebastian Leuzinger
> University of Basel, Department of Environmental Science
> Institute of Botany
> Schönbeinstr. 6 CH-4056 Basel
> ph0041 (0) 61 2673511
> fax   0041 (0) 61 2673504
> email [EMAIL PROTECTED]
> web   http://pages.unibas.ch/botschoen/leuzinger
>

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] write data from function into external table

2006-06-14 Thread Niels Vestergaard Jensen
On Wed, 14 Jun 2006, Sebastian Leuzinger wrote:

> Thanks for the hints, they are useful to me. However, I still do not
> understand why my approach fails. In a user defined function, R does not seem
> to want to write into an object defined outside this function. (see my first
> example below). I guess there is some logic behind this.

I think the general programming concept you are looking for is "scope",
see sec. 10.7 in the R intro:

http://cran.r-project.org/doc/manuals/R-intro.html#Scope

There's more (and more technical) about it in the R Language Definition.

best

Niels


> On Wednesday 14 June 2006 14:22, you wrote:
> > Here are two alternatives.  See ?"<<-"
> >
> > testfct1 <- function() test[1] <<- 100
> >
> > # following one can be written more compactly as
> > #   testfct2 <- function(test) replace(test, 1, 100)
> > testfct2 <- function(test) { test[1] <- 100; test }
> >
> > # test
> > test <- 1:3; testfct1(); test
> > test <- 1:3; test <- testfct2(test); test
> >
> > On 6/14/06, Sebastian Leuzinger <[EMAIL PROTECTED]> wrote:
> > > Dear list,
> > > My apologies if a solution / explanation to this already exists on the
> > > list, but it is difficult to assign it to a certain keyword.
> > >
> > > test<-c(1:3)
> > > testfct <- function(x) {test[1]<-100}
> > >  test
> > > [1] 1 2 3
> > >  testfct(1)
> > > [1] 1 2 3
> > >
> > > Basically, I would like to write data into an external table that the
> > > function does not know. Why is this not working / what alternatives
> > > exist?
> > >
> > > Thanks, Sebastian
> > >
> > > 
> > > Sebastian Leuzinger
> > > University of Basel, Department of Environmental Science
> > > Institute of Botany
> > > Schönbeinstr. 6 CH-4056 Basel
> > > ph0041 (0) 61 2673511
> > > fax   0041 (0) 61 2673504
> > > email [EMAIL PROTECTED]
> > > web   http://pages.unibas.ch/botschoen/leuzinger
> > >
> > > __
> > > R-help@stat.math.ethz.ch mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide!
> > > http://www.R-project.org/posting-guide.html
>
> --
> 
> Sebastian Leuzinger
> University of Basel, Department of Environmental Science
> Institute of Botany
> Schönbeinstr. 6 CH-4056 Basel
> ph0041 (0) 61 2673511
> fax   0041 (0) 61 2673504
> email [EMAIL PROTECTED]
> web   http://pages.unibas.ch/botschoen/leuzinger
>
> __
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] write data from function into external table

2006-06-14 Thread Sebastian Leuzinger
Thanks for the hints, they are useful to me. However, I still do not 
understand why my approach fails. In a user defined function, R does not seem 
to want to write into an object defined outside this function. (see my first 
example below). I guess there is some logic behind this.

On Wednesday 14 June 2006 14:22, you wrote:
> Here are two alternatives.  See ?"<<-"
>
> testfct1 <- function() test[1] <<- 100
>
> # following one can be written more compactly as
> #   testfct2 <- function(test) replace(test, 1, 100)
> testfct2 <- function(test) { test[1] <- 100; test }
>
> # test
> test <- 1:3; testfct1(); test
> test <- 1:3; test <- testfct2(test); test
>
> On 6/14/06, Sebastian Leuzinger <[EMAIL PROTECTED]> wrote:
> > Dear list,
> > My apologies if a solution / explanation to this already exists on the
> > list, but it is difficult to assign it to a certain keyword.
> >
> > test<-c(1:3)
> > testfct <- function(x) {test[1]<-100}
> >  test
> > [1] 1 2 3
> >  testfct(1)
> > [1] 1 2 3
> >
> > Basically, I would like to write data into an external table that the
> > function does not know. Why is this not working / what alternatives
> > exist?
> >
> > Thanks, Sebastian
> >
> > 
> > Sebastian Leuzinger
> > University of Basel, Department of Environmental Science
> > Institute of Botany
> > Schönbeinstr. 6 CH-4056 Basel
> > ph0041 (0) 61 2673511
> > fax   0041 (0) 61 2673504
> > email [EMAIL PROTECTED]
> > web   http://pages.unibas.ch/botschoen/leuzinger
> >
> > __
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html

-- 

Sebastian Leuzinger
University of Basel, Department of Environmental Science
Institute of Botany
Schönbeinstr. 6 CH-4056 Basel
ph0041 (0) 61 2673511
fax   0041 (0) 61 2673504
email [EMAIL PROTECTED] 
web   http://pages.unibas.ch/botschoen/leuzinger

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] write data from function into external table

2006-06-14 Thread Gabor Grothendieck
Here are two alternatives.  See ?"<<-"

testfct1 <- function() test[1] <<- 100

# following one can be written more compactly as
#   testfct2 <- function(test) replace(test, 1, 100)
testfct2 <- function(test) { test[1] <- 100; test }

# test
test <- 1:3; testfct1(); test
test <- 1:3; test <- testfct2(test); test

On 6/14/06, Sebastian Leuzinger <[EMAIL PROTECTED]> wrote:
> Dear list,
> My apologies if a solution / explanation to this already exists on the list,
> but it is difficult to assign it to a certain keyword.
>
> test<-c(1:3)
> testfct <- function(x) {test[1]<-100}
>  test
> [1] 1 2 3
>  testfct(1)
> [1] 1 2 3
>
> Basically, I would like to write data into an external table that the function
> does not know. Why is this not working / what alternatives exist?
>
> Thanks, Sebastian
>
> 
> Sebastian Leuzinger
> University of Basel, Department of Environmental Science
> Institute of Botany
> Schönbeinstr. 6 CH-4056 Basel
> ph0041 (0) 61 2673511
> fax   0041 (0) 61 2673504
> email [EMAIL PROTECTED]
> web   http://pages.unibas.ch/botschoen/leuzinger
>
> __
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] write data from function into external table

2006-06-14 Thread Adaikalavan Ramasamy
What is your desired output ? This will clarify the problem greatly.

Perhaps, this might be of some use :

 f <- function(v, pos, val=100){  v[pos] <- val; return(v)  }

 test <- 1:3
 test <- f(test, 1)
 test
 [1] 100  2  3

Regards, ADai



On Wed, 2006-06-14 at 12:41 +0200, Sebastian Leuzinger wrote:
> Dear list,
> My apologies if a solution / explanation to this already exists on the list, 
> but it is difficult to assign it to a certain keyword.
> 
> test<-c(1:3)
> testfct <- function(x) {test[1]<-100}
>  test
> [1] 1 2 3
>  testfct(1)
> [1] 1 2 3
> 
> Basically, I would like to write data into an external table that the 
> function 
> does not know. Why is this not working / what alternatives exist?
> 
> Thanks, Sebastian 
> 
> 
> Sebastian Leuzinger
> University of Basel, Department of Environmental Science
> Institute of Botany
> Schönbeinstr. 6 CH-4056 Basel
> ph0041 (0) 61 2673511
> fax   0041 (0) 61 2673504
> email [EMAIL PROTECTED] 
> web   http://pages.unibas.ch/botschoen/leuzinger
> 
> __
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Re: [R] write data from function into external table

2006-06-14 Thread Dimitris Rizopoulos
maybe you're looking for something like,

test <- 1:3
testfct <- function(x){
x[1] <- 100
x
}

test
testfct(1)
testfct(test)


I hope it helps.

Best,
Dimitris


Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
 http://www.student.kuleuven.be/~m0390867/dimitris.htm


- Original Message - 
From: "Sebastian Leuzinger" <[EMAIL PROTECTED]>
To: 
Sent: Wednesday, June 14, 2006 12:41 PM
Subject: [R] write data from function into external table


Dear list,
My apologies if a solution / explanation to this already exists on the 
list,
but it is difficult to assign it to a certain keyword.

test<-c(1:3)
testfct <- function(x) {test[1]<-100}
 test
[1] 1 2 3
 testfct(1)
[1] 1 2 3

Basically, I would like to write data into an external table that the 
function
does not know. Why is this not working / what alternatives exist?

Thanks, Sebastian


Sebastian Leuzinger
University of Basel, Department of Environmental Science
Institute of Botany
Schönbeinstr. 6 CH-4056 Basel
ph0041 (0) 61 2673511
fax   0041 (0) 61 2673504
email [EMAIL PROTECTED]
web   http://pages.unibas.ch/botschoen/leuzinger

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] write data from function into external table

2006-06-14 Thread Sebastian Leuzinger
Dear list,
My apologies if a solution / explanation to this already exists on the list, 
but it is difficult to assign it to a certain keyword.

test<-c(1:3)
testfct <- function(x) {test[1]<-100}
 test
[1] 1 2 3
 testfct(1)
[1] 1 2 3

Basically, I would like to write data into an external table that the function 
does not know. Why is this not working / what alternatives exist?

Thanks, Sebastian 


Sebastian Leuzinger
University of Basel, Department of Environmental Science
Institute of Botany
Schönbeinstr. 6 CH-4056 Basel
ph0041 (0) 61 2673511
fax   0041 (0) 61 2673504
email [EMAIL PROTECTED] 
web   http://pages.unibas.ch/botschoen/leuzinger

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html