[R] String manipulation

Eric Lecoutre lecoutre at stat.ucl.ac.be
Fri Jun 25 11:30:58 CEST 2004


Hi,

Just to show there are always different ways to handle such problems:

You could also prepare a matrix (or data.frame) containing those data (in 
fact, I suspect you do have data in that form).

 > names1 <- letters[1:10]
 > names2 <- letters[1:20]
 > data <- cbind(genre=rep(c("female","male"),c(10,20)), 
names=c(names1,names2))
 > data
       genre    names
  [1,] "female" "a"
  [2,] "female" "b"
  #[...] continued

 > split(data[,"names"],data[,"genre"])
$female
  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

$male
  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" 
"r" "s" "t"

See ?split

Eric


At 11:16 25/06/2004, Dimitris Rizopoulos wrote:
>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
>
>______________________________________________
>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

Eric Lecoutre
UCL /  Institut de Statistique
Voie du Roman Pays, 20
1348 Louvain-la-Neuve
Belgium

tel: (+32)(0)10473050
lecoutre at stat.ucl.ac.be
http://www.stat.ucl.ac.be/ISpersonnel/lecoutre

If the statistics are boring, then you've got the wrong numbers. -Edward 
Tufte




More information about the R-help mailing list