[R] Student

Hans Ekbrand hans.ekbrand at sociology.gu.se
Wed Apr 8 10:41:45 CEST 2009


On Wed, Apr 08, 2009 at 10:02:10AM +0200, alberto cassese wrote:
> Hi,
> I have  problem. In the function below (test and test2) i want the function
> test not to print the variable data but i want the function test2 to use the
> variable test$data.
>
> This is the creation of the variable data:
> 
> > matrice=c(1:10)
> > matrice=matrix(matrice,nrow=5,ncol=2)
> 
> This is the function test:
> 
> > test=function(data){
> + return(list(x=5,data=data))
> + }
> 
> This is the function test2:
> 
> > test2=function(list){
> + bodri=list$data
> + bodri[1,2]=bodri[2,2]+1
> + return(bodri)
> + }
> 
> Below there are the result:
> 
> > uno=test(matrice)
> > due=test2(uno)
> > uno
> $x
> [1] 5
> 
> $data
>      [,1] [,2]
> [1,]    1    6
> [2,]    2    7
> [3,]    3    8
> [4,]    4    9
> [5,]    5   10
> 
> > due
>      [,1] [,2]
> [1,]    1    8
> [2,]    2    7
> [3,]    3    8
> [4,]    4    9
> [5,]    5   10
> 
> 
> What i want is:
> 
> > uno=test(matrice)
> > due=test2(uno)
> > uno
> $x
> [1] 5

x is a variable, 5 is variable data and you don't want variable data
printed?

> > due
>      [,1] [,2]
> [1,]    1    8
> [2,]    2    7
> [3,]    3    8
> [4,]    4    9
> [5,]    5   10
> 

Use uno[1], either directly or by creating a third variable from uno[1]

> one.and.a.half <- uno[1]
> one.and.a.half
$x
[1] 5

Or, if you *really* want what that printed output from test(matrice),
create a class for your list-object, and add a special print method,
that will only print the first item of the list.


-- 
Hans Ekbrand (http://sociologi.cjb.net) <hans at sociologi.cjb.net>
A. Because it breaks the logical sequence of discussion
Q. Why is top posting bad?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090408/70f5536c/attachment-0002.bin>


More information about the R-help mailing list