Hello, I have a vector, "dates", as a series of 3 digit elements, i.e. > date [1] 528 528 528 528 528 528 528 528 528 528 528 528 708 708 708 708 708 708 [19] 708 708 708 708 529 529 529 529 529 529 529 529 529 529 529 529 529 529 [37] 529 624 I need to convert them into julian, but have to insert a "/" or "-" after the first number within each element of the vector (5/28 5/28 etc). Found plenty functions to replace by a pattern but not to cut by a certain number of digits with an element. Alternately, if I could run all the elements into one long vector and then cut every one then two digits, that would work as well. Thanks. -- View this message in context: http://r.789695.n4.nabble.com/Cut-a-within-elements-by-length-not-value-of-vectors-tp2290440p2290440.html Sent from the R help mailing list archive at Nabble.com.
Martyn Byng
2010-Jul-15 17:33 UTC
[R] Cut a within elements by length, not value, of vectors
Hi, Is this what you are looking for? a = c(528,528,528,528,708,708,529,1208,423,1506,321) paste(substr(a,1,nchar(a)-2),substr(a,nchar(a)-1,nchar(a)),sep="/") [1] "5/28" "5/28" "5/28" "5/28" "7/08" "7/08" "5/29" "12/08" "4/23" "15/06" "3/21" Martyn -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of btc1 Sent: 15 July 2010 18:22 To: r-help at r-project.org Subject: [R] Cut a within elements by length, not value, of vectors Hello, I have a vector, "dates", as a series of 3 digit elements, i.e. > date [1] 528 528 528 528 528 528 528 528 528 528 528 528 708 708 708 708 708 708 [19] 708 708 708 708 529 529 529 529 529 529 529 529 529 529 529 529 529 529 [37] 529 624 I need to convert them into julian, but have to insert a "/" or "-" after the first number within each element of the vector (5/28 5/28 etc). Found plenty functions to replace by a pattern but not to cut by a certain number of digits with an element. Alternately, if I could run all the elements into one long vector and then cut every one then two digits, that would work as well. Thanks. -- View this message in context: http://r.789695.n4.nabble.com/Cut-a-within-elements-by-length-not-value- of-vectors-tp2290440p2290440.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. ________________________________________________________________________ This e-mail has been scanned for all viruses by Star.\ _...{{dropped:12}}
Erik Iverson
2010-Jul-15 17:40 UTC
[R] Cut a within elements by length, not value, of vectors
btc1 wrote:> Hello, I have a vector, "dates", as a series of 3 digit elements, i.e. > date > [1] 528 528 528 528 528 528 528 528 528 528 528 528 708 708 708 708 708 > 708 > [19] 708 708 708 708 529 529 529 529 529 529 529 529 529 529 529 529 529 > 529 > [37] 529 624 > > I need to convert them into julian, but have to insert a "/" or "-" after > the first number within each element of the vector (5/28 5/28 etc). Found > plenty functions to replace by a pattern but not to cut by a certain number > of digits with an element. Alternately, if I could run all the elements into > one long vector and then cut every one then two digits, that would work as > well.And what about the year? You might have better luck using the Date class in R. #not tested as.Date("05282010", format = "%m%d%Y")
gsub("(.*)(.{2})","\\1/\\2",dates) ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/Cut-a-within-elements-by-length-not-value-of-vectors-tp2290440p2290471.html Sent from the R help mailing list archive at Nabble.com.
Henrique Dallazuanna
2010-Jul-15 17:57 UTC
[R] Cut a within elements by length, not value, of vectors
Try this: gsub("^(.)", "\\1/", dates) On Thu, Jul 15, 2010 at 2:21 PM, btc1 <btcaldwell@berkeley.edu> wrote:> > Hello, I have a vector, "dates", as a series of 3 digit elements, i.e. > > date > [1] 528 528 528 528 528 528 528 528 528 528 528 528 708 708 708 708 708 > 708 > [19] 708 708 708 708 529 529 529 529 529 529 529 529 529 529 529 529 529 > 529 > [37] 529 624 > > I need to convert them into julian, but have to insert a "/" or "-" after > the first number within each element of the vector (5/28 5/28 etc). Found > plenty functions to replace by a pattern but not to cut by a certain number > of digits with an element. Alternately, if I could run all the elements > into > one long vector and then cut every one then two digits, that would work as > well. > > Thanks. > -- > View this message in context: > http://r.789695.n4.nabble.com/Cut-a-within-elements-by-length-not-value-of-vectors-tp2290440p2290440.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Nikhil Kaza
2010-Jul-15 18:07 UTC
[R] Cut a within elements by length, not value, of vectors
Building on Erik's solution and because it would easier to do date arithmetic.. d1 <- as.character(date) d1 <- ifelse(nchar(d1)<4, paste(0,d1,sep=""),d1) d2 <- as.Date(date, "%m%d") On Jul 15, 2010, at 1:21 PM, btc1 wrote:> > Hello, I have a vector, "dates", as a series of 3 digit elements, > i.e. > date > [1] 528 528 528 528 528 528 528 528 528 528 528 528 708 708 708 708 > 708 > 708 > [19] 708 708 708 708 529 529 529 529 529 529 529 529 529 529 529 529 > 529 > 529 > [37] 529 624 > > I need to convert them into julian, but have to insert a "/" or "-" > after > the first number within each element of the vector (5/28 5/28 etc). > Found > plenty functions to replace by a pattern but not to cut by a certain > number > of digits with an element. Alternately, if I could run all the > elements into > one long vector and then cut every one then two digits, that would > work as > well. > > Thanks. > -- > View this message in context: http://r.789695.n4.nabble.com/Cut-a-within-elements-by-length-not-value-of-vectors-tp2290440p2290440.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.
Apparently Analagous Threads
- who can tell me the reason why it is different on calculating Moran's I using ARCGIS, Geoda and R?
- Re: *** buffer overflow detected *** accessing invalid FD in libguestfs
- Environment change?
- Branch 'as' - 4 commits - libswfdec/swfdec_as_interpret.c test/trace
- lm function (PR#13608)