moleps
2010-Jun-06 19:51 UTC
[R] paste together a string object later to be utilized in a function
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
Joris Meys
2010-Jun-07 00:50 UTC
[R] paste together a string object later to be utilized in a function
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
Greg Snow
2010-Jun-07 20:31 UTC
[R] paste together a string object later to be utilized in a function
Does the collapse argument to the paste function do what you want? Possibly nested inside another paste. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of moleps > Sent: Sunday, June 06, 2010 1:51 PM > To: r-help at r-project.org > Subject: [R] paste together a string object later to be utilized in a > function > > 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.
moleps
2010-Jun-07 20:57 UTC
[R] paste together a string object later to be utilized in a function
Sorry for bothering all of you. In the end it turned out to be much simpler than
I thought. Takes a while to get used to the vectorizing idea.
require(survival)
require(ggplot2)
ggkm<-function(time,event,stratum) {
stratum<-as.factor(stratum)
m2s<-Surv(time,as.numeric(event))
fit <- survfit(m2s ~ stratum)
w<-fit$time
k<-fit$surv
o<-length(levels(stratum))
strata<-c(rep(names(fit$strata[1:o]),fit$strata[1:o]))
upper<-fit$upper
lower<-fit$lower
f<-data.frame(w,k,strata,upper,lower)
r<-ggplot
(f,aes(x=w,y=k,fill=strata,group=strata))+geom_line(aes(color=strata))+geom_ribbon(aes(ymin=lower,ymax=upper),alpha=0.3)+xlim(0,fit$maxtime)+ylim(0,1)
r<-r+scale_fill_brewer(f$strata,palette="Set1")+scale_color_brewer(f$strata,palette="Set1")
return(r)
}
data(lung)
with(lung,ggkm(time,status,sex))
with(lung,ggkm(time,status,pat.karno))
//M
On 7. juni 2010, at 22.31, Greg Snow wrote:
> Does the collapse argument to the paste function do what you want?
Possibly nested inside another paste.
>
> --
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> greg.snow at imail.org
> 801.408.8111
>
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>> project.org] On Behalf Of moleps
>> Sent: Sunday, June 06, 2010 1:51 PM
>> To: r-help at r-project.org
>> Subject: [R] paste together a string object later to be utilized in a
>> function
>>
>> 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.