[R] paste together a string object later to be utilized in a function

Joris Meys jorismeys at gmail.com
Mon Jun 7 12:39:08 CEST 2010


It helps if you provide sample data, so we can try some things out
ourselves. Right now, I can't run your function without guessing how
your data might look like.

Yet, try something like this :
> ts <- list(a=1:5)

> names(ts$a) <- letters[1:5]

> n <- length(names(ts$a))

> tmp <- sapply(1:n,function(i){rep(names(ts$a[i]),ts$a[i])})

> strata <- unlist(tmp)
> strata
 [1] "a" "b" "b" "c" "c" "c" "d" "d" "d" "d" "e" "e" "e" "e" "e"

Cheers
Joris



On Mon, Jun 7, 2010 at 10:26 AM, moleps islon <moleps2 at gmail.com> wrote:
> I'm sorry for not clearer describing my motive with this--
>
> So this is what I'm trying to do- Take a survival object and utilize
> it in ggplot.
>
> ggkm<-function(time,event,stratum) {
>
>
>    m2s<-Surv(time,as.numeric(event))
>
>    fit <- survfit(m2s ~ stratum)
>
>    f$time<-fit$time
>
>    f$surv<-fit$surv
>
>    f$strata<-c(rep(names(fit$strata[1]),fit$strata[1]),rep(names(fit$strata[2]),fit$strata[2]))
>
>    f$upper<-fit$upper
>    f$lower<-fit$lower
>
>    r<-ggplot (f,aes(x=time,y=surv,fill=strata,group=strata))+geom_line()+geom_ribbon(aes(ymin=lower,ymax=upper),alpha=0.3)
>    return(r)
> }
>
>
> My problem is that I can have more than two strata, and I would like
> the function to automatically detect the number of strata. Hence a
> quick hack as to how I can create f$strata when I dont know the number
> of strata would be appreciated.
>
>
> The paste("rep(names(fit$strata[",1:length(fit$strata),"]),fit$strata[",1:length(fit$strata),"])",sep="")
> is as close as I get. However this results in multiple strings and I
> havent discovered yet how I can pass this so f$strata is created. This
> could easily be done in stata appending to a macro, but as I am a
> recent convertee I dont know how  to do this in R. (yet...?)
>
>
> Regards,
>
> //M
>
> On Mon, Jun 7, 2010 at 2:50 AM, Joris Meys <jorismeys at gmail.com> wrote:
>> Wild guess, but it looks like you are looking at :
>>
>>> ts <- list(a=1:5)
>>> names(ts$a) <- letters[1:5]
>>> v<-paste("rep(names(ts$a[",1:b,"]),ts$a[",1:b,"])",sep="")
>>>
>>> sapply(v,function(x){eval(parse(,text=x))})
>> $`rep(names(ts$a[1]),ts$a[1])`
>> [1] "a"
>>
>> $`rep(names(ts$a[2]),ts$a[2])`
>> [1] "b" "b"
>>
>> $`rep(names(ts$a[3]),ts$a[3])`
>> [1] "c" "c" "c"
>>
>> $`rep(names(ts$a[4]),ts$a[4])`
>> [1] "d" "d" "d" "d"
>>
>> $`rep(names(ts$a[5]),ts$a[5])`
>> [1] "e" "e" "e" "e" "e"
>>
>>> assign("test",eval(parse(text=v[3])))
>>> test
>> [1] "c" "c" "c"
>>>
>>
>> Cheers
>> Joris
>>
>> On Sun, Jun 6, 2010 at 9:51 PM, moleps <moleps2 at gmail.com> wrote:
>>> Dear r-listers,
>>>
>>> I need to pass a string to a function. However the length of the string is dependent on the length of a vector.
>>>
>>>
>>> b<-length(h)
>>> v<-paste("rep(names(ts$a[",1:b,"]),ts$a[",1:b,"])",sep="")
>>>
>>>
>>> Is it possible somehow to pass this as an argument to a function later on ?
>>>
>>>
>>>
>>> Regards,
>>>
>>> //M
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>>
>>
>> --
>> Ghent University
>> Faculty of Bioscience Engineering
>> Department of Applied mathematics, biometrics and process control
>>
>> tel : +32 9 264 59 87
>> Joris.Meys at Ugent.be
>> -------------------------------
>> Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
>>
>



-- 
Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php



More information about the R-help mailing list