[R] Converting a character vector to numeric

Rui Barradas ruipbarradas at sapo.pt
Sat Mar 23 21:01:41 CET 2013


Hello,

Try the following. It issues a warning because of the second as.numeric. 
(It still has the parenthesis.)


ifelse(grepl("\\(", Vec), -as.numeric(sub("\\((.*)\\)", "\\1", Vec)), 
as.numeric(Vec))


Hope this helps,

Rui Barradas

Em 23-03-2013 19:51, Christofer Bogaso escreveu:
> Hello again,
>
> Let say I have following vector:
>
> Vec <- c("0.0365780769", "(1.09738648244378)", "(0.812507787221523)",
> "0.5778069963", "(0.452456601362355)", "-1.8900812605", "-1.8716093762",
> "0.0055217041", "-0.4769192333", "-2.4133018880")
>
>
> Now I want to convert this vector to numeric vector. I am having
> problem to doing so because there are some elements with "()" which
> should be treated as negative.
>
>
> Can somebody help me how to achieve that?
>
> Thanks and regards,
>
> ______________________________________________
> 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