Skip to content

how to grep in r

4 messages · Tammy Ma, Anthony Damico, arun +1 more

#
Hi,

#A lengthy solution for the simple problem.

#dat1 is the dataset
res<-unlist(lapply(strsplit(dat1[,1],split="-"),function(x) if(length(x)==1) rep(x,2) else x))
res[seq(from=2,to=length(res),by=2)]

#or
gsub("[-]","",unlist(regmatches(dat1[,1],gregexpr("-.*|.*\\+",dat1[,1]))))
A.K.



----- Original Message -----
From: Tammy Ma <metal_licaling at live.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Wednesday, December 12, 2012 8:40 AM
Subject: [R] how to grep in r


Hi, 


I met this problem.

?  Trade_Price_Band? ? ? ?  x
1? ? ? ? ? ? ? 0-30 0.6237240
2? ? ? ? ?  101-150 0.6743857
3? ? ? ? ?  151-200 0.6778513
4? ? ? ? ?  201-300 0.6640293
5? ? ? ? ?  301-400 0.6630991
6? ? ? ? ? ?  31-50 0.6314547
7? ? ? ? ?  401-500 0.6776249
8? ? ? ? ? ? ? 500+ 0.6557705
9? ? ? ? ? ?  51-75 0.6621073
10? ? ? ? ?  76-100 0.6623469


I want to get the following matrix

?  Trade_Price_Band? ? ? ?  x
1? ? ? ? ?  30? ? ? ? ? ? 0.6237240
2? ? ? ? ? 150? ? ? ? ? 0.6743857
3? ? ? ? ? 200? ? ? ?  0.6778513
4? ? ? ? ? 300? ? ? ? ? 0.6640293


how can I get this in r?


Thanks.

Tammy

??? ???  ??? ?  ??? ??? ? 
??? [[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.
#
Something like 

tbp <- as.numeric( gsub("(.*-)?([0-9]+)\\+?","\\2", Trade_Price_Band) ) 

would get you the numbers you want. The '500+' is then treated as 500, though. One way of getting round that might be

tbp <- as.numeric( gsub("(.*-)?([0-9]+)","\\2", Trade_Price_Band) ) 

then 

tbp[is.na(tbp)] <- 501 #or some other number bigger than 500


----- Original Message -----
From: Tammy Ma <metal_licaling at live.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Wednesday, December 12, 2012 8:40 AM
Subject: [R] how to grep in r


Hi,


I met this problem.

   Trade_Price_Band         x
1              0-30 0.6237240
2           101-150 0.6743857
3           151-200 0.6778513
4           201-300 0.6640293
5           301-400 0.6630991
6             31-50 0.6314547
7           401-500 0.6776249
8              500+ 0.6557705
9             51-75 0.6621073
10           76-100 0.6623469


I want to get the following matrix

   Trade_Price_Band         x
1           30            0.6237240
2          150          0.6743857
3          200         0.6778513
4          300          0.6640293


how can I get this in r?


Thanks.

Tammy



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


______________________________________________
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 email and any attachments are confidential. Any use...{{dropped:8}}