[R] Extract an number from a character

David Wolfskill r at catwhisker.org
Mon Nov 23 21:30:59 CET 2015


On Mon, Nov 23, 2015 at 12:20:53PM -0800, Sam Albers wrote:
> Hello,
> 
> I have a problem to which I am certain grep or gsub or that family of
> functions are the solution. However, I just can't seem to wrap my mind
> around exactly how. I have a dataframe below that has the dimensions
> of a net. I am given the data is the "W X H" format. For calculations
> I'll like to have each number as a separated column. I have been using
> ifelse(). However that seems like a poor solution to this problem
> especially once dataframes get larger and larger.
> 
> So my question is, can anyone describe a way to extract the number
> from the variable y below is the example? I had also tried substr()
> but that fall apart with the 2.5 x 2.5 net.
> 
> 
> Thanks in advance!
> 
> Sam
> 
> Example:
> ##dataframe
> df<-data.frame(x=rnorm(10),
>                y=c("7 x 3","7 x 3","7 x 3","7 x 3","7 x 3","2.5 x
> 2.5","2.5 x 2.5","2.5 x 2.5","2.5 x 2.5","2.5 x 2.5"))
> 
> 
> df$Width<-as.numeric(ifelse(df$y=="7 x 3","7","2.5"))
> df$Height<-as.numeric(ifelse(df$y=="7 x 3","3","2.5"))
> 
> 
> df$Width<-as.numeric(substr(df$y,5,5))
> df$Width<-as.numeric(substr(df$y,5,5))
> ....

Something like:

> df$Height <- as.numeric(sub(' x .*$', '', df$y))
> df$Width <- as.numeric(sub('^.* x ', '', df$y))
> df
            x         y Height Width
1   1.2118958     7 x 3    7.0   3.0
2  -0.3911277     7 x 3    7.0   3.0
3  -0.8933737     7 x 3    7.0   3.0
4  -0.6537011     7 x 3    7.0   3.0
5   2.6182771     7 x 3    7.0   3.0
6   0.9622942 2.5 x 2.5    2.5   2.5
7   1.2858848 2.5 x 2.5    2.5   2.5
8   1.0431044 2.5 x 2.5    2.5   2.5
9  -1.4957406 2.5 x 2.5    2.5   2.5
10  2.1751108 2.5 x 2.5    2.5   2.5

seems promising.

Peace,
david
-- 
David H. Wolfskill				r at catwhisker.org
Those who would murder in the name of God or prophet are blasphemous cowards.

See http://www.catwhisker.org/~david/publickey.gpg for my public key.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 949 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20151123/b8b7047a/attachment.bin>


More information about the R-help mailing list