Re: [R] String manipulation

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Thu 20 Oct 2005 - 23:56:42 EST

you could use "gsub()", i.e.,

strg <- c("II11", "II18", "II23", "III1", "III13", "III16", "III19", "III2", "III7", "IV10", "IV11", "IV12")
#########
x <- as.numeric(gsub("[^0-9]", "", strg)) y <- gsub("[0-9]", "", strg)
tapply(x, y, mean)

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


> R-help,
>
> I have a data frame which contains a character string column that is
> something like;
>
> II11
> II18
> II23
> III1
> III13
> III16
> III19
> III2
> III7
> IV10
> IV11
> IV12
> IX16
> IX4
> V12
> V18
> V2
> V20
> V23
> V4
> VII14
> VII18
> VII21
> VII26
> VII28
> VII33
> VII4
> VII48
> VII5
> ....
> ....
> ....
>
> I want to apply a function (e.g mean) by grouping according to the
> roman part of the string, i.e,
>
> by I
> by V
> by VII
> ...
> ...
> and so on.
>
> I have looked at string manipulation functions (grep, pmatch,,,) but
> I
> can't really get it the way I want.
> Can anyone help?
>
> Thanks in advance.
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Oct 20 23:59:09 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 19:10:58 EST