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]]
assuming you want more than just the first four rows tpb <- read.table( text = " 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") tpb[ , 1 ] <- gsub(".*-", "", tpb[ , 1 ] ) tpb On Wed, Dec 12, 2012 at 8:40 AM, Tammy Ma <metal_licaling@live.com> wrote:> > 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@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. >[[alternative HTML version deleted]]
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.