[R] accessing return variables from a function

Eik Vettorazzi E.Vettorazzi at uke.uni-hamburg.de
Fri Jul 9 12:57:06 CEST 2010


which is just
unlist(sort(x, index.return=T))

but I think, Noah would like to have something like
srt<-sort(x, index.return=T)
names(srt)<-c("y","indexes")
attach(srt)

but that is wasteful either.


Am 09.07.2010 12:28, schrieb David Winsemius:
>
> On Jul 9, 2010, at 5:20 AM, Noah Silverman wrote:
>
>> Hi,
>>
>> I am trying to figure out a "short" way to access two values output from
>> the sort function.
>>
>>> x <- c(3,4,3,6,78,3,1,2)
>>> sort(x, index.return=T)
>> $x
>> [1]  1  2  3  3  3  4  6 78
>>
>> $ix
>> [1] 7 8 1 3 6 2 4 5
>>
>>
>> It would be great to do something like this (doesn't work.):
>
> > yoursort <- function(x) {sx <- sort(x, index.return=T); return(
> c(sx$x, sx$ix) )}
> > yoursort(x)
>  [1]  1  2  3  3  3  4  6 78  7  8  1  3  6  2  4  5
>
>> c(y, indexes) <- sort(x, index.return=T)
>>
>> But that doesn't work.
>>
>> I CAN grab the output of sort in a variable and then access it twice to
>> get the values, but that seems wasteful.
>>
>> Any  ideas?
>>
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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.

-- 
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790



More information about the R-help mailing list