0

I'm trying to create values in a single column, each on a different row. I have 4 sites, 4 times, and 5 hydrophones to label in this one column. Surely there's a nice quick way to repeat these (other than manually)?

I'd like it to look like:

S1 T1 H1
S1 T1 H2
S1 T1 H3
S1 T1 H4
S1 T1 H5
S1 T2 H1
S1 T2 H2
...
S4 T4 H5

I have to do this fairly regularly with different levels/values so I'd love a quicker way to do it. Feel free to direct me to a page that has already discussed this, that I may have missed.

Edit/clarification: I would like these values in one column, not each in a separate column. The other pages that this linked to only showed results in multiple columns which is of no use to me. (at least that's what I understood, I am still a new user)

  • Mods please remove [duplicate] from this - the linked questions DO NOT answer my question and I am now getting frustrated. Or, can you explain how the linked questions answer my question? – Chris Karaconstantis Apr 19 '17 at 09:34

1 Answers1

0

Try expand.grid:

expand.grid(Col1=paste0("S",(1:4)),
            Col2=paste0("T",(1:4)),
            Col3=paste0("H",(1:5))) 
zx8754
  • 52,746
  • 12
  • 114
  • 209
r.user.05apr
  • 5,356
  • 3
  • 22
  • 39
  • This is close, thanks. However that makes the S column increase first, and the H column increase last, is there a way to make the H column increase first? – Chris Karaconstantis Apr 19 '17 at 06:59
  • eg<-expand.grid(Col1=paste0("S",(1:4)), Col2=paste0("T",(1:4)), Col3=paste0("H",(1:5))); eg[order(eg$Col1,eg$Col2,eg$Col3),] – r.user.05apr Apr 19 '17 at 07:23
  • Great, that's the desired order, is there a simple way of combining that into 1 column not 3? – Chris Karaconstantis Apr 19 '17 at 07:33
  • Thanks r.user.05apr, I appreciate the answer but it wasn't really what I was looking for. No thanks at all to zx8754 for closing this without reading what I was asking. Yes the questions were similar, but you have now wasted half my evening trying to understand and track down further information. I understand you need to prevent multiple postings as the forums would fill up very quickly, but have some f***ing patience next time. Turns out what I was looking for was: – Chris Karaconstantis Apr 19 '17 at 10:05
  • mydf <- data.frame(matrix(ncol = 4))
    mydf <- CJ(Col1 = paste0("S", 1:4), Col2 = paste0("T", 1:4), Col3 = paste0("H", 1:5), Col4 = "")
    mydf[,4] <- with(eg, Col1, Col2, sep = " ")
    mydf <- mydf[,4]
    – Chris Karaconstantis Apr 19 '17 at 10:05
  • eg$Combined=apply(eg,1,function(x) paste(x,collapse="")) .. apply makes it easier in a way I think. – r.user.05apr Apr 19 '17 at 10:23