0

Haven't had much success with googling this one. New R user. Suspect the answer is with reshape/plyr but haven't been able to figure it out.

I have data in this form: sampling interval, recorded value, # times value was recorded

e.g

45.3, 17, 3 
48, 19, 2

Doing a histogram of the data, I just get a histogram containing 17, 19. When really my data is 17,17,17,19,19

I can write a loop to go through the times recorded column and create a new data column, but it's awkward and I'm sure I'm over-thinking this.

Is there a simple function to turn this into a data frame that would be:

45.3, 17 
45.3, 17 
45.3, 17 
48, 19
48, 19
agstudy
  • 119,832
  • 17
  • 199
  • 261
mat4nier
  • 262
  • 4
  • 14
  • Close viote explanation: I think the answer that Michael Lawrence provided to a very similar answer was better since it mentioned rep() and barplot(). – IRTFM May 23 '14 at 23:40
  • Perfect. You are right. As far as stackoverflow rules go, once something has been marked as duplicate, is there anything the original author needs to do to agree? Certainly don't want to clutter up the place. – mat4nier May 23 '14 at 23:59
  • There really is no mechanism for proper merging of questions and answers. There can be removal, but that typically is reserved for offensive postings or utterly clueless or or egregious violation of the norms of the site. Even the answer that @agstudy "deleted" is still visible to those of us with a few more rep. – IRTFM May 24 '14 at 16:01

1 Answers1

3

Use dat$V3 to indicate how often you need each row of dat, by feeding it into the times parameter of rep():

>  dat <- read.table(text='45.3, 17, 3 
  + 48, 19, 2',sep=',')
> dat[rep(1:nrow(dat),times=dat$V3),]
      V1 V2 V3
1   45.3 17  3
1.1 45.3 17  3
1.2 45.3 17  3
2   48.0 19  2
2.1 48.0 19  2
Stephan Kolassa
  • 7,953
  • 2
  • 28
  • 48