Hello togehter, i have a litte problem to convert a data.frame. My data.frame looks like this one A 1 R5000 2 R4800 3 R4700 4 3500 5 3800 I need now a command, which outputs all the numbers, without the character in front. The solution look like this one: A 1 5000 2 4800 3 4700 4 3500 5 3800 Thanks. Best regards. Mat -- View this message in context: http://r.789695.n4.nabble.com/substring-if-value-starts-with-a-character-tp4690823.html Sent from the R help mailing list archive at Nabble.com.
Hello, Try the following. dat <- read.table(text = " A 1 R5000 2 R4800 3 R4700 4 3500 5 3800 ", header = TRUE) dat dat2 <- dat dat2[] <- lapply(dat, function(x) sub("^[[:alpha:]]*(.*$)", "\\1", x)) dat2 Hope this helps, Rui Barradas Em 19-05-2014 14:00, Mat escreveu:> Hello togehter, > > i have a litte problem to convert a data.frame. My data.frame looks like > this one > A > 1 R5000 > 2 R4800 > 3 R4700 > 4 3500 > 5 3800 > > I need now a command, which outputs all the numbers, without the character > in front. > The solution look like this one: > > A > 1 5000 > 2 4800 > 3 4700 > 4 3500 > 5 3800 > > Thanks. > > Best regards. Mat > > > > -- > View this message in context: http://r.789695.n4.nabble.com/substring-if-value-starts-with-a-character-tp4690823.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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 Mat, Try that: gsub("[A-Za-z]", "", df$A) This is a regular expression that looks for all characters (A to Z and a to z) and replaces it with nothing ("") You will need to convert the column to numeric after this operation. HTH, Ivan -- Ivan Calandra University of Franche-Comt? Laboratoire Chrono-Environnement Bureau ATER -107L 16, Route de Gray 25030 Besan?on Cedex, France ivan.calandra at univ-fcomte.fr +33 (0) 381 66 20 60 http://chrono-environnement.univ-fcomte.fr/spip.php?article1830 Le 19/05/14 15:00, Mat a ?crit :> Hello togehter, > > i have a litte problem to convert a data.frame. My data.frame looks like > this one > A > 1 R5000 > 2 R4800 > 3 R4700 > 4 3500 > 5 3800 > > I need now a command, which outputs all the numbers, without the character > in front. > The solution look like this one: > > A > 1 5000 > 2 4800 > 3 4700 > 4 3500 > 5 3800 > > Thanks. > > Best regards. Mat > > > > -- > View this message in context: http://r.789695.n4.nabble.com/substring-if-value-starts-with-a-character-tp4690823.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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: ?dat <- read.table(text="A 1? R5000 2? R4800 3? R4700 4? 3500 5? 3800",sep="",header=TRUE,stringsAsFactors=FALSE) ?dat$A <- as.numeric(gsub("[[:alpha:]]+","",dat$A)) A.K. On Monday, May 19, 2014 9:02 AM, Mat <matthias.weber at fnt.de> wrote: Hello togehter, i have a litte problem to convert a data.frame. My data.frame looks like this one ? ? ? A 1? R5000 2? R4800 3? R4700 4? 3500 5? 3800 I need now a command, which outputs all the numbers, without the character in front. The solution look like this one: ? ? ? ? A 1? 5000 2? 4800 3? 4700 4? 3500 5? 3800 Thanks. Best regards. Mat -- View this message in context: http://r.789695.n4.nabble.com/substring-if-value-starts-with-a-character-tp4690823.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.