0

I believe this is fairly simple, although I am new to using R and code. I have a dataset which has a single row for each rodent trap site. There were however, 8 occasions of trapping over 4 years. What I wish to do is to expand the trap site data and append a number 1 to 8 for each row.

Then I can then label them with the trap visit for a subsequent join with the obtained trap data.

I have managed to replicate the rows with the following code. And while the rows are expanded in the data frame to 1, 1.1...1.7,2, 2.1...2.7 etc. I cannot figure out how to convert this to a useable column based ID.

structure(list(TrapCode = c("IA1sA", "IA2sA", "IA3sA", "IA4sA", 
"IA5sA"), Y = c(-12.1355987315, -12.1356879776, -12.1357664998, 
-12.1358823313, -12.1359720852), X = c(-69.1335789865, -69.1335225279, 
-69.1334668485, -69.1333847769, -69.1333226532)), row.names = c(NA, 
5L), class = "data.frame")

gps_1 <– gps_1[rep(seq_len(nrow(gps_1)), 3), ]

gives

"IA5sA", "IA1sA", "IA2sA", "IA3sA", "IA4sA", "IA5sA", "IA1sA", 
"IA2sA", "IA3sA", "IA4sA", "IA5sA"), Y = c(-12.1355987315, -12.1356879776, 
-12.1357664998, -12.1358823313, -12.1359720852, -12.1355987315, 
-12.1356879776, -12.1357664998, -12.1358823313, -12.1359720852, 
-12.1355987315, -12.1356879776, -12.1357664998, -12.1358823313, 
-12.1359720852), X = c(-69.1335789865, -69.1335225279, -69.1334668485, 
-69.1333847769, -69.1333226532, -69.1335789865, -69.1335225279, 
-69.1334668485, -69.1333847769, -69.1333226532, -69.1335789865, 
-69.1335225279, -69.1334668485, -69.1333847769, -69.1333226532
)), row.names = c("1", "2", "3", "4", "5", "1.1", "2.1", "3.1", 
"4.1", "5.1", "1.2", "2.2", "3.2", "4.2", "5.2"), class = "data.frame")

I have a column with Trap_ID currently being a unique identifier. I hope that after the replication I could append an iteration number to this to keep it as a unique ID.

For example:

Trap_ID
IA1sA.1
IA1sA.2
IA1sA.3
IA2sA.1
IA2sA.2
IA2sA.3
DidDrog11
  • 51
  • 6
  • A minimal reproducible example would be a great start ! https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – desval Jun 20 '19 at 18:13
  • ... if you are looking to add 1, ..., 8 as another column to the dataframe, gps, then try ... gps$Trap_ID <- 1:8. – greengrass62 Jun 20 '19 at 18:17
  • @tino_ladino This is the dataframe example. ```structure(list(TrapCode = c("IA1sA", "IA2sA", "IA3sA", "IA4sA", "IA5sA"), Y = c(-12.1355987315, -12.1356879776, -12.1357664998, -12.1358823313, -12.1359720852), X = c(-69.1335789865, -69.1335225279, -69.1334668485, -69.1333847769, -69.1333226532)), row.names = c(NA, 5L), class = "data.frame")``` – DidDrog11 Jun 20 '19 at 19:21

1 Answers1

1

Simply use a cross join (i.e., join with no by columns to return a cartesian product of both sets):

mdf <- merge(data.frame(Trap_ID = 1:8), trap_side_df, by=NULL)
Parfait
  • 104,375
  • 17
  • 94
  • 125