[R] separate a variable in several variables

CE.KA ce.kaya75 at yahoo.fr
Thu Nov 13 11:27:29 CET 2008


Thank you DAVID, GABOR, PHIL, WACEK, PETER and GREG
I tried:
Data2 <- data.frame(x=DATA$x, y = gsub('[^[:digit:]]', '', DATA$x), z=
gsub('[^[:alpha:]]', '', DATA$x))
and everything is ok


Wacek Kusnierczyk wrote:
> 
> 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
> 
> ______________________________________________
> 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.
> 
> 

-- 
View this message in context: http://www.nabble.com/separate-a-variable-in-several-variables-tp20469491p20478171.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list