[R] problem with function

Rui Barradas ruipbarradas at sapo.pt
Wed Jul 18 10:26:25 CEST 2012


Hello,

I believe that the answer to your final question is yes but you are 
making a really big confusion of what you want, of how you are 
explaining it and of the way R works.

First, I have written a function to read the data from the post, without 
creating a disk file. In order to be complete, I'll repost everything:



f <- function(){
x <- read.csv2(text="
Id;fc;zat;dat;hat
PG;1,3;1;2;3
HU;2,5;0;2;5
JZ;1,8;1;0;4
", header=TRUE)
x
}

# your function, one line changed.

read.data <- function(file,  ...){

#  mydata <- read.csv2 (file, header=TRUE, as.is=TRUE) # read everything
   mydata <- f()

   fc <- mydata[,2]                      # extract FC column
   rownames(mydata) <- mydata[,1]        # same for gene names to be used
                                         # as rownames
   mydata <- mydata[,-(1:2)]             # drop those two columns
   attr(mydata, 'fc') <- fc              # attach FC back as attribute
   return(mydata)                        # and return
   # return fc or return attr(mydata, 'fc') does not work  (???)
}

# further code is like this:

newdata <- read.data("testfile")
head(newdata)       # works but not
fc                  # or (why should it?)
attr(newdata, 'fc') # it does not work if I include return(fc) or (???)
                     # attr... in my function neither

attributes(newdata) # This is new, see ALL attributes.


I've also ncluded some (???) in the comments. See below inline.

Em 17-07-2012 22:13, Hermann Norpois escreveu:
> Dear list,
>
> I have a problem with defining a function (see below) to read my testfile
> (see testfile). My function only returns mydata I wish to work with
> attr(mydata, 'fc') as well (for labelling a plot). Principally it works if
> I do not insist on this function but it would be much easer if it is
> possible to return mydata AND attr(mydata, 'fc') by using a function.
>
> 1) testfile:
>
> Id;fc;zat;dat;hat
> PG;1,3;1;2;3
> HU;2,5;0;2;5
> JZ;1,8;1;0;4
>
>
> 2) my function:
>
> read.data <- function(file,  ...){
>
>    mydata <- read.csv2 (file, header=TRUE, as.is=TRUE) # read everything
>
>    fc <- mydata[,2]                      # extract FC column
>    rownames(mydata) <- mydata[,1]        # same for gene names to be used as
> rownames
>    mydata <- mydata[,-(1:2)]             # drop those two columns
>    attr(mydata, 'fc') <- fc               # attach FC back as attribute
>    return(mydata)                        # and return
>    # return fc or return attr(mydata, 'fc') does not work
> }

What doesn't work? Why should it? If you return fc or attr() you lose 
mydata, In R what goes on inside a function stays there unless it s 
returned.
Your return(mydata) in the previous line does what you want!!!
Meaning, it reutrns mydata with it's attributes, including fc.

>
> further code is like this:
>
> newdata <- read.data("testfile")
> head (newdata) #works but not
> fc # or
> attr(mydata, 'fc') # it does not work if I include return fc or attr... in
> my function neither
>

Of course it doesn't work, mydata does NOT exist here, it only exists 
inside the function read.data(). The function returns the VALUE OF 
mydata, not the variable itself. R works in a call/return by value basis.

Then you assign the return value to newdata. So what you want is 
attr(newdata, 'fc') like my code asks for, and it does work.

>
> Then I can get access to newdata and on but not on fc or attr(mydata, 'fc').
>
> Is there a possibility to use function and having access to attr AND
> newdata?

Yes.

But no longer to mydata. Its value yes, it's now in newdata. You are 
obviuosly mistaking one for the other. Do not worry. Everything is 
working like it should. You are correctly reading the data in, assigning 
row names, assigning an attribute called fc with some decimal values in 
it and correctly returning what you need be returned.

Everything is allright. Read my code and run it.

Hope this helps,

Rui Barradas

>
> Thanks
> Sincerely
> Hermann Norpois
>
> 	[[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.
>



More information about the R-help mailing list