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,
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. >
Hi, Try this: Vec[grepl("\\(",Vec)]<-paste0("-",gsub("[()]","",Vec[grepl("\\(",Vec)])) as.numeric(Vec) # [1]? 0.036578077 -1.097386482 -0.812507787? 0.577806996 -0.452456601 ?#[6] -1.890081260 -1.871609376? 0.005521704 -0.476919233 -2.413301888 A.K. ----- Original Message ----- From: Christofer Bogaso <bogaso.christofer at gmail.com> To: r-help <r-help at r-project.org> Cc: Sent: Saturday, March 23, 2013 3:51 PM Subject: [R] Converting a character vector to numeric 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.
Use gsub to convert the parenthesized expressions. newvec <- gsub("\\((.+)\\)", "-\\1", Vec) If you have to do much of this, you should learn about regular expressions -- there are many good tutorials on the Web -- and stop asking for help here. Cheers, Bert On Sat, Mar 23, 2013 at 12:51 PM, Christofer Bogaso < bogaso.christofer@gmail.com> wrote:> 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@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. >-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]]
On Mar 23, 2013, at 12:51 PM, Christofer Bogaso wrote:> 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")If this is to be done at the stage of data-inpuyt then definition of an appropriate as.method and reference to that method in colClasses would be the way to go. There are several worked examples in the archives. setClass("Accounting") setAs("character", "Accounting", function(from) as.numeric( gsub("[$(),]", "", from)) ) contribs <- read.table(text=Vec, colClasses=c(CTRIB_AMT="Accounting")) contribs V1 1 0.036578077 2 1.097386482 3 0.812507787 4 0.577806996 5 0.452456601 6 -1.890081260 7 -1.871609376 8 0.005521704 9 -0.476919233 10 -2.413301888 -- David Winsemius Alameda, CA, USA