Hi list, I'm sure the explanation must be laughably simple to the experts out there, but I just could figure it out. I have a simple data frame that looks like,>head(da.off)DDate OffP 1 2005-01-01 41.23 2 2005-01-02 44.86 3 2005-01-03 44.86 4 2005-01-04 43.01 5 2005-01-05 45.47 6 2005-01-06 48.62 where the first column DDate currently is character, and OffP is numeric. I want to duplicate every row 2 times, so I thought I use apply(), x <- apply(da.off, 2, rep, each=2) The result is a matrix of all character, head(x) DDate OffP 1 "2005-01-01" " 41.23" 1 "2005-01-01" " 41.23" 2 "2005-01-02" " 44.86" 2 "2005-01-02" " 44.86" 3 "2005-01-03" " 44.86" 3 "2005-01-03" " 44.86" To convert it back to numeric, I did x <- as.data.frame(x) x$OffP <- as.numeric(x$OffP) However, the OffP column didn't convert correctly, a mystery since they "look" quite alright above. (I know, I know, there seems to be a space there. But why?) head(x) DDate OffP 1 2005-01-01 150 1.1 2005-01-01 150 2 2005-01-02 202 2.1 2005-01-02 202 3 2005-01-03 202 3.1 2005-01-03 202 Is this the wrong way to use apply or rep? Horace
The approach here is to perform the repetition on the indices (or rownames) rather than on the data frame directly. Using the builtin data frame BOD any of the following would work: BOD[gl(nrow(BOD), 2),] BOD[rep(1:nrow(BOD), each = 2),] BOD[rep(rownames(BOD), each = 2),] On 8/11/06, Horace Tso <Horace.Tso at pgn.com> wrote:> Hi list, > > I'm sure the explanation must be laughably simple to the experts out > there, but I just could figure it out. I have a simple data frame that > looks like, > > >head(da.off) > DDate OffP > 1 2005-01-01 41.23 > 2 2005-01-02 44.86 > 3 2005-01-03 44.86 > 4 2005-01-04 43.01 > 5 2005-01-05 45.47 > 6 2005-01-06 48.62 > > where the first column DDate currently is character, and OffP is > numeric. > > I want to duplicate every row 2 times, so I thought I use apply(), > > x <- apply(da.off, 2, rep, each=2) > > The result is a matrix of all character, > > head(x) > DDate OffP > 1 "2005-01-01" " 41.23" > 1 "2005-01-01" " 41.23" > 2 "2005-01-02" " 44.86" > 2 "2005-01-02" " 44.86" > 3 "2005-01-03" " 44.86" > 3 "2005-01-03" " 44.86" > > To convert it back to numeric, I did > > x <- as.data.frame(x) > x$OffP <- as.numeric(x$OffP) > > However, the OffP column didn't convert correctly, a mystery since they > "look" quite alright above. (I know, I know, there seems to be a space > there. But why?) > > head(x) > DDate OffP > 1 2005-01-01 150 > 1.1 2005-01-01 150 > 2 2005-01-02 202 > 2.1 2005-01-02 202 > 3 2005-01-03 202 > 3.1 2005-01-03 202 > > Is this the wrong way to use apply or rep? > > Horace > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
The trick is to rep() the index, not the data: R> dat2 <- dat[rep(1:nrow(dat), each=2), ] R> dat2 DDate OffP 1 2005-01-01 41.23 1.1 2005-01-01 41.23 2 2005-01-02 44.86 2.1 2005-01-02 44.86 3 2005-01-03 44.86 3.1 2005-01-03 44.86 4 2005-01-04 43.01 4.1 2005-01-04 43.01 5 2005-01-05 45.47 5.1 2005-01-05 45.47 6 2005-01-06 48.62 6.1 2005-01-06 48.62 Andy From: Horace Tso> > Hi list, > > I'm sure the explanation must be laughably simple to the > experts out there, but I just could figure it out. I have a > simple data frame that looks like, > > >head(da.off) > DDate OffP > 1 2005-01-01 41.23 > 2 2005-01-02 44.86 > 3 2005-01-03 44.86 > 4 2005-01-04 43.01 > 5 2005-01-05 45.47 > 6 2005-01-06 48.62 > > where the first column DDate currently is character, and OffP > is numeric. > > I want to duplicate every row 2 times, so I thought I use apply(), > > x <- apply(da.off, 2, rep, each=2) > > The result is a matrix of all character, > > head(x) > DDate OffP > 1 "2005-01-01" " 41.23" > 1 "2005-01-01" " 41.23" > 2 "2005-01-02" " 44.86" > 2 "2005-01-02" " 44.86" > 3 "2005-01-03" " 44.86" > 3 "2005-01-03" " 44.86" > > To convert it back to numeric, I did > > x <- as.data.frame(x) > x$OffP <- as.numeric(x$OffP) > > However, the OffP column didn't convert correctly, a mystery > since they "look" quite alright above. (I know, I know, there > seems to be a space there. But why?) > > head(x) > DDate OffP > 1 2005-01-01 150 > 1.1 2005-01-01 150 > 2 2005-01-02 202 > 2.1 2005-01-02 202 > 3 2005-01-03 202 > 3.1 2005-01-03 202 > > Is this the wrong way to use apply or rep? > > Horace > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > >
Thanks Gabor, Andy, and Phil. I learn new trick, particularly the use of gl(). H.>>> "Gabor Grothendieck" <ggrothendieck at gmail.com> 8/11/2006 12:01 PM >>>The approach here is to perform the repetition on the indices (or rownames) rather than on the data frame directly. Using the builtin data frame BOD any of the following would work: BOD[gl(nrow(BOD), 2),] BOD[rep(1:nrow(BOD), each = 2),] BOD[rep(rownames(BOD), each = 2),] On 8/11/06, Horace Tso <Horace.Tso at pgn.com> wrote:> Hi list, > > I'm sure the explanation must be laughably simple to the experts out > there, but I just could figure it out. I have a simple data framethat> looks like, > > >head(da.off) > DDate OffP > 1 2005-01-01 41.23 > 2 2005-01-02 44.86 > 3 2005-01-03 44.86 > 4 2005-01-04 43.01 > 5 2005-01-05 45.47 > 6 2005-01-06 48.62 > > where the first column DDate currently is character, and OffP is > numeric. > > I want to duplicate every row 2 times, so I thought I use apply(), > > x <- apply(da.off, 2, rep, each=2) > > The result is a matrix of all character, > > head(x) > DDate OffP > 1 "2005-01-01" " 41.23" > 1 "2005-01-01" " 41.23" > 2 "2005-01-02" " 44.86" > 2 "2005-01-02" " 44.86" > 3 "2005-01-03" " 44.86" > 3 "2005-01-03" " 44.86" > > To convert it back to numeric, I did > > x <- as.data.frame(x) > x$OffP <- as.numeric(x$OffP) > > However, the OffP column didn't convert correctly, a mystery sincethey> "look" quite alright above. (I know, I know, there seems to be aspace> there. But why?) > > head(x) > DDate OffP > 1 2005-01-01 150 > 1.1 2005-01-01 150 > 2 2005-01-02 202 > 2.1 2005-01-02 202 > 3 2005-01-03 202 > 3.1 2005-01-03 202 > > Is this the wrong way to use apply or rep? > > Horace > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. >______________________________________________ R-help at stat.math.ethz.ch 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.