Frank Duan
2006-Sep-25 16:04 UTC
[R] Splitting a character variable into a numeric one and a character one?
Hi All, I have a data with a variable like this: Column 1 "123abc" "12cd34" "1e23" ... Now I want to do an operation that can split it into two variables: Column 1 Column 2 Column 3 "123abc" 123 "abc" "12cd34" 12 "cd34" "1e23" 1 "e23" ... So basically, I want to split the original variabe into a numeric one and a character one, while the splitting element is the first character in Column 1. I searched the forum with key words "strsplit"and "substr", but still can't solve this problem. Can anyone give me some hints? Thanks in advance, FD [[alternative HTML version deleted]]
Gabor Grothendieck
2006-Sep-25 16:28 UTC
[R] Splitting a character variable into a numeric one and a character one?
strapply in package gsubfn can do that:
library(gsubfn)
s <- c("123abc", "12cd34", "1e23")
out <- strapply(s, "^([[:digit:]]+)(.*)", c)
out <- do.call(rbind, out) # as a matrix
data.frame(x = out[,1], num = as.numeric(out[,2]), char = out[,3]) #
as a data.frame
On 9/25/06, Frank Duan <fhduan at gmail.com>
wrote:> Hi All,
>
> I have a data with a variable like this:
>
> Column 1
>
> "123abc"
> "12cd34"
> "1e23"
> ...
>
> Now I want to do an operation that can split it into two variables:
>
> Column 1 Column 2 Column 3
>
> "123abc" 123 "abc"
> "12cd34" 12 "cd34"
> "1e23" 1 "e23"
> ...
>
> So basically, I want to split the original variabe into a numeric one and a
> character one, while the splitting element is the first character in Column
> 1.
>
> I searched the forum with key words "strsplit"and
"substr", but still can't
> solve this problem. Can anyone give me some hints?
>
> Thanks in advance,
>
> FD
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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
> and provide commented, minimal, self-contained, reproducible code.
>
Marc Schwartz (via MN)
2006-Sep-25 16:30 UTC
[R] Splitting a character variable into a numeric one and a character one?
On Mon, 2006-09-25 at 11:04 -0500, Frank Duan wrote:> Hi All, > > I have a data with a variable like this: > > Column 1 > > "123abc" > "12cd34" > "1e23" > ... > > Now I want to do an operation that can split it into two variables: > > Column 1 Column 2 Column 3 > > "123abc" 123 "abc" > "12cd34" 12 "cd34" > "1e23" 1 "e23" > ... > > So basically, I want to split the original variabe into a numeric one and a > character one, while the splitting element is the first character in Column > 1. > > I searched the forum with key words "strsplit"and "substr", but still can't > solve this problem. Can anyone give me some hints? > > Thanks in advance, > > FDSomething like this using gsub() should work I think:> DFV1 1 123abc 2 12cd34 3 1e23 # Replace letters and any following chars with "" DF$V2 <- gsub("[A-Za-Z]+.*", "", DF$V1) # Replace any initial numbers with "" DF$V3 <- gsub("^[0-9]+", "", DF$V1)> DFV1 V2 V3 1 123abc 123 abc 2 12cd34 12 cd34 3 1e23 1 e23 See ?gsub and ?regex for more information. HTH, Marc Schwartz