Hello R users, Imagine the data frame DATA with the variable x which is composed by numeric and alpha caracters.> DATAx 1 12F 2 13 AD 3 356PO 4 1D 5 GRT 6 PO52 7 LN4Z Is there a way to separarate x in 2 variables: y: only numeric caracters z: only alpha caracters For exemple: x y z 1 12F 12 F 2 13 AD 13 AD 3 356PO 356 PO 4 1D 1 D 5 GRT 0 GRT 6 PO52 52 PO 7 LN4Z 4 LNZ Kind regards -- View this message in context: http://www.nabble.com/separate-a-variable-in-several-variables-tp20469491p20469491.html Sent from the R help mailing list archive at Nabble.com.
Tena koe
Try:
gsub('[[:alpha:]]', '', unlist(DATA)) and
gsub('[[:digit:]]', '', unlist(DATA))
unlist(DATA) since it looks like DATA maybe a dataframe.
You are still left with the spaces, but these can be readily removed.
Someone else may offer a better solution.
HTH ...
Peter Alspach
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of CE.KA
> Sent: Thursday, 13 November 2008 10:10 a.m.
> To: r-help at r-project.org
> Subject: [R] separate a variable in several variables
>
>
> Hello R users,
> Imagine the data frame DATA with the variable x which is
> composed by numeric and alpha caracters.
>
> > DATA
> x
> 1 12F
> 2 13 AD
> 3 356PO
> 4 1D
> 5 GRT
> 6 PO52
> 7 LN4Z
>
> Is there a way to separarate x in 2 variables:
> y: only numeric caracters
> z: only alpha caracters
> For exemple:
> x y z
> 1 12F 12 F
> 2 13 AD 13 AD
> 3 356PO 356 PO
> 4 1D 1 D
> 5 GRT 0 GRT
> 6 PO52 52 PO
> 7 LN4Z 4 LNZ
>
> Kind regards
> --
> View this message in context:
> http://www.nabble.com/separate-a-variable-in-several-variables
> -tp20469491p20469491.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
The contents of this e-mail are privileged and/or confidential to the named
recipient and are not to be used by any other person and/or organisation.
If you have received this e-mail in error, please notify the sender and delete
all material pertaining to this e-mail.
CE.KA wrote:> > x > 1 12F > 2 13 AD > 3 356PO > 4 1D > 5 GRT > 6 PO52 > 7 LN4Z > > Is there a way to separarate x in 2 variables: > y: only numeric caracters > z: only alpha caracters > For exemple: > x y z > 1 12F 12 F > 2 13 AD 13 AD > 3 356PO 356 PO > 4 1D 1 D > 5 GRT 0 GRT > 6 PO52 52 PO > 7 LN4Z 4 LNZ > >if you need it quickly, here is a hack. there may be better ways. # some rubbish data d = data.frame(x = replicate(10, paste(sample(c(letters, 0:9, " "), 10), collapse=""))) patterns = paste("[", c("^", ""), "A-Za-z]| ", sep="") for (i in 1:2) d[patterns[i]] = gsub(patterns[i], "", d$x) tweak the regex patterns accordingly to your demand for whitespace etc. rename the columns as you like. vQ