Hi, let's see, if someone can help my with this one: I have the string as follows:> str<-("one","two","three")Now I want to concatenate the items to one string, seperateted by space or something else,>str >"one, two, three"If possible without a loop. My actual goal ist to create string like>str.names >"female = names1, male = names2"and pass it as argument to list(), intending to create a list>names.list<-list( female = names1, male = names2)Thanks a lot, Robin
Hi Robin,
regarding you first question you could use,
str <- c("one","two","three")
paste(str, collapse=", ")
hoewver, describing what you actually want to do I'd use,
names1 <- letters[1:10]
names2 <- letters[1:20]
lis <- lapply(1:2, function(x) get(paste("names", x,
sep="")))
names(lis) <- c("female", "male")
lis
I hope this helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Doctoral Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/396887
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Robin Gruna" <robin_gruna at hotmail.com>
To: <r-help at stat.math.ethz.ch>
Sent: Friday, June 25, 2004 11:07 AM
Subject: [R] String manipulation
> Hi,
> let's see, if someone can help my with this one:
> I have the string as follows:
> > str<-("one","two","three")
>
> Now I want to concatenate the items to one string, seperateted by
space or> something else,
> >str
> >"one, two, three"
> If possible without a loop.
>
> My actual goal ist to create string like
> >str.names
> >"female = names1, male = names2"
> and pass it as argument to list(), intending to create a list
> >names.list<-list( female = names1, male = names2)
>
> Thanks a lot,
> Robin
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
Hi look at ?paste> paste("one","two","three",sep=",")[1] "one,two,three" On Friday 25 June 2004 11:07, Robin Gruna wrote:> Hi, > let's see, if someone can help my with this one: > > I have the string as follows: > > str<-("one","two","three") > > Now I want to concatenate the items to one string, seperateted by space or > something else, > > >str > >"one, two, three" > > If possible without a loop. > > My actual goal ist to create string like > > >str.names > >"female = names1, male = names2" > > and pass it as argument to list(), intending to create a list > > >names.list<-list( female = names1, male = names2) > > Thanks a lot, > Robin > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
Does the following do what you want (or supply the missing gaps):
> Args <- paste(c("female", "male"), "=
names", 1:2, sep="")
> Args
[1] "female= names1" "male= names2"
> listText <- paste("names.list<-list(",
+ paste(Args, collapse=","), ")")
> listText
[1] "names.list<-list( female= names1,male= names2 )"
> names1 <- c("Sue", "Betty")
> names2 <- c("Bill", "Sam")
> eval(parse(text=listText))
> names.list
$female
[1] "Sue" "Betty"
$male
[1] "Bill" "Sam"
hope this helps. spencer graves
p.s. "str" is the name of a function to "Compactly Display the
Structure of an Arbitrary R Object". I prefer to avoid masking
functions with local objects, even though R can tell which object is
desired in many but not all contexts. To avoid this, I often test a
name before I use it. When I don't get, "Object ... not found", I
try
something different, e.g., using capital letters somewhere or adding a
"." at the end. This also has the advantage of introducing me or
reminding me of functions I may not have known.
Robin Gruna wrote:
>Hi,
>let's see, if someone can help my with this one:
>I have the string as follows:
>
>
>>str<-("one","two","three")
>>
>>
>
>Now I want to concatenate the items to one string, seperateted by space or
>something else,
>
>
>>str
>>"one, two, three"
>>
>>
>If possible without a loop.
>
>My actual goal ist to create string like
>
>
>>str.names
>>"female = names1, male = names2"
>>
>>
>and pass it as argument to list(), intending to create a list
>
>
>>names.list<-list( female = names1, male = names2)
>>
>>
>
>Thanks a lot,
>Robin
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
>
>