[R] reexpr transform nonumeric values to numeric

arun smartpink111 at yahoo.com
Tue Aug 21 14:50:43 CEST 2012


HI,
Try this:
as.numeric(gsub("^([0-9].[0-9]{1,4})-$","-\\1",temp))
#or
as.numeric(gsub("^([[:digit:]].[[:digit:]]{1,4})-$","-\\1",temp))
#or
as.numeric(gsub("^([[:digit:]]..*{1,4})-$","-\\1",temp))
#or
as.numeric(gsub("^(.*.[[:digit:]]{1,4})-$","-\\1",temp))
A.K.




----- Original Message -----
From: PIKAL Petr <petr.pikal at precheza.cz>
To: r-help <r-help at stat.math.ethz.ch>
Cc: 
Sent: Tuesday, August 21, 2012 6:16 AM
Subject: [R] reexpr transform nonumeric values to numeric

Dear all

I have got this kind of data

temp <- structure(c(1L, 1L, 1L, 1L, 1L, 16L, 6L, 6L, 16L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 11L, 10L, 16L, 16L, 
16L, 21L, 16L, 16L, 16L, 1L, 1L, 1L, 16L, 16L, 16L, 16L, 1L, 
1L, 7L, 3L, 1L, 1L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 4L, 10L, 8L, 
2L, 1L, 4L, 6L, 2L, 1L, 1L, 1L, 9L, 4L, 10L, 1L, 1L, 1L, 1L, 
1L, 1L, 6L, 4L, 6L, 6L, 8L, 8L, 6L, 6L, 6L, 10L, 11L, 1L, 1L, 
2L, 4L, 2L, 7L, 10L, 2L, 16L, 10L, 6L, 10L, 1L, 4L, 3L, 17L, 
1L, 2L, 1L, 1L, 2L, 1L, 2L, 3L, 2L, 6L, 8L, 4L, 1L, 1L, 8L, 8L, 
6L, 3L, 4L, 8L, 6L, 4L, 2L, 6L, 2L, 4L, 6L, 4L, 4L, 2L, 6L, 4L, 
2L, 3L, 4L, 6L, 8L, 8L, 10L, 6L, 4L, 10L, 4L, 4L, 4L, 2L, 4L, 
4L, 2L, 8L, 10L, 11L, 11L, 10L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 13L, 13L, 14L, 8L, 3L, 5L, 3L, 3L, 1L, 1L, 1L, 1L, 4L, 
1L, 12L, 16L, 26L, 16L, 1L, 1L, 1L, 1L, 16L, 16L, 16L, 16L, 16L, 
16L, 16L, 14L, 15L, 15L, 18L, 23L, 20L, 17L, 20L, 19L, 24L, 24L, 
24L, 23L, 20L, 17L, 17L, 17L, 27L, 17L, 17L, 22L, 17L, 17L, 24L, 
22L, 22L, 19L, 23L, 23L, 25L, 5L, 28L), .Label = c("0.0000", 
"0.0100", "0.0100-", "0.0200", "0.0200-", "0.0300", "0.0300-", 
"0.0400", "0.0400-", "0.0500", "0.0600", "0.0600-", "0.0700", 
"0.0800", "0.0900", "0.1000", "0.1000-", "0.1100", "0.1100-", 
"0.1200-", "0.1300", "0.1300-", "0.1400-", "0.1500-", "0.1800-", 
"0.2000", "0.2000-", "0.4000-"), class = "factor")

As you can see, negative values have minus sign at the end of value (strange but sometimes used).

I can find those values by
grep("-",levels(temp)) and manipulate it to strip "-" sign, put "-"sign before the value by paste and change it to numeric
as.numeric(paste("-", gsub("-", "", levels(temp)[grep("-",levels(temp))]), sep=""))

but Is there any kind of possible regular expression to do it in one step? E.g. I would like to end with some function which can take whole vector temp and change it to numeric.

Something like

as.numeric(gsub("some clever regular expression", temp))

Thank you
Petr

______________________________________________
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.




More information about the R-help mailing list