[R] Replace element with pattern

arun smartpink111 at yahoo.com
Mon Nov 4 15:43:23 CET 2013


Hi,
Using ?gsub()
gsub("[^0-9]+"," ",a$Col1)

#or
 gsub("[^0-9]+(,|/|)"," ",a$Col1) 

library(stringr)
dat <- read.table(text=str_trim(gsub("[^0-9]+(,|/|)"," ",a$Col1)),sep="")


A.K.







On Monday, November 4, 2013 2:22 AM, "mohan.radhakrishnan at polarisFT.com" <mohan.radhakrishnan at polarisFT.com> wrote:

Please ignore. This works. 

str_extract_all(a$Col1,"[0-9]+(,|/|)") 

Mohan 



From:      
 mohan.radhakrishnan at polarisft.com 
To:      
 arun <smartpink111 at yahoo.com> 
Cc:      
 R help <r-help at r-project.org> 
Date:      
 11/04/2013 12:22 PM 
Subject:    
   Re: [R] Replace
element with pattern 
Sent by:    
   r-help-bounces at r-project.org 
________________________________



Hi,

   How do we extract the numbers into an array or data frame
assuming the 
rows of text is in a data frame ?

> head(a)
Col1
1 Current Usage : init:2359296 
used:15857920,committed:15892480,max:50331648|
2 Current Usage : init:2359296 
used:15857920,committed:15892480,max:50331648|
> 

The working pattern is this [0-9]*(,|\|)

I tried

gsub("[0-9]*(,|/|)","",a$Col1)

> grep("[0-9]*(,|/|)",a$Col1,value=TRUE)
[1] "Current Usage : init:2359296 
used:15857920,committed:15892480,max:50331648|"
[2] "Current Usage : init:2359296 
used:15857920,committed:15892480,max:50331648|"
>

> str_extract(a$Col1,"[0-9]*(,|/|)")
[1] "" ""


Thanks



From:   arun <smartpink111 at yahoo.com>
To:     R help <r-help at r-project.org>
Cc:     "mohan.radhakrishnan at polarisft.com" 
<mohan.radhakrishnan at polarisft.com>
Date:   11/01/2013 06:40 PM
Subject:        Re: [R] Replace element with pattern





Hi,

Try this:

Lines1 <- readLines(textConnection("Peak Usage    : init:2359296, 
used:15859328, committed:15892480,max:50331648Current Usage : init:2359296 
used:15857920,committed:15892480,max:50331648|-------------------|
Peak Usage    : init:2359296, used:15859328, 
committed:15892480,max:50331648Current Usage : init:2359296 
used:15857920,committed:15892480,max:50331648|-------------------|"))

data.frame(Col1=as.matrix(gsub("(.*?[/|])","\\1",Lines1)))
#Assuming that 
you want to read it from Peak Usage to the first "|":

#If it is from Current Usage to "|"

data.frame(Col1=as.matrix(gsub("^.*(Current.*?[/|]).*","\\1",Lines1)))


A.K.


On Friday, November 1, 2013 4:11 AM, "mohan.radhakrishnan at polarisft.com" 
<mohan.radhakrishnan at polarisft.com> wrote:
Hi,
        I have a data frame with one column and several
rows of the form.

"Peak Usage    : init:2359296, used:15859328, committed:15892480, 
max:50331648Current Usage : init:2359296, used:15857920, 
committed:15892480, max:50331648|-------------------|"

I tested the regex 

Current.*?[\|]

in an online tester which greedily matches upto the first 'pipe' character

Current Usage : init:2359296, used:15857920, committed:15892480, 
max:50331648|

This is what I want.

I tried to replace the entire rows using 

apply( y, 1, function(x) gsub(x,"Current.*?[/|]",x)) which didn't
work.

How is this done ? I also want to recursively apply some more patterns
one 

by one on the rows till I reduce it to exactly what I want. Is there a
way 

to do this without loops ?

Thanks,
Mohan


This e-Mail may contain proprietary and confidential information and is 
sent for the intended recipient(s) only.  If by an addressing or 
transmission error this mail has been misdirected to you, you are 
requested to delete this mail immediately. You are also hereby notified 
that any use, any form of reproduction, dissemination, copying, 
disclosure, modification, distribution and/or publication of this e-mail 
message, contents or its attachment other than by its intended recipient/s 
is strictly prohibited.

Visit us at http://www.polarisFT.com

   [[alternative HTML version deleted]]

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






This e-Mail may contain proprietary and confidential information and is
sent for the intended recipient(s) only.  If by an addressing or transmission
error this mail has been misdirected to you, you are requested to delete
this mail immediately. You are also hereby notified that any use, any form
of reproduction, dissemination, copying, disclosure, modification, distribution
and/or publication of this e-mail message, contents or its attachment other
than by its intended recipient/s is strictly prohibited.

Visit us at 
http://www.polarisFT.com


[[alternative HTML version deleted]]

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



________________________________
This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only.  If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately. You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited. Visit us at http://www.polarisFT.com

________


More information about the R-help mailing list