I have to combine 5 files with the same structure and add a new variable to index the new data frame, but all 5 files are using the same ID.

I successfully combine them but I do not find how to index them. I have tried a few loops, but they were not giving me what I wanted.

# Combining files
path <- "D:/..."
filenames <- list.files(path)
t <- do.call("rbind", lapply(filenames, read.table, header = TRUE))
# Trying indexing with loops:
for (i in 1:length(t$ID){
  t$ID2<-(t$ID+last(t$ID2)) 
}

I have 5 files, all of them with the same structure, and all of them using the same variable for identification, i.e. file 1 would have:

ID: 1 1 1 2 2 2 3 3 3

And file 2 to 5 would have exactly the same IDs: I would like to combine them into a single data frame so I would have this:

ID: 1 1 1 2 2 2 3 3 3 1 1 1 2 2 2 3 3 3 1 1 1....

and then name them differently. So I would have:

ID: 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7...

1 Answers

0
Roman Luštrik On Best Solutions

How's this? This code finds the largest ID of first (i) data.frame and then adds that to IDs of next (i+1) data.frame. It records (i+1) largest ID and uses that in the (i+2) data.frame.

For this to work, you will have to forego the first do.call(rbind, ...) in your code.

xy1 <- data.frame(id = rep(1:4, each = 4), matrix(runif(4*4 * 3), ncol = 3))
xy2 <- data.frame(id = rep(1:7, each = 3), matrix(runif(3*7 * 3), ncol = 3))
xy3 <- data.frame(id = rep(1:3, each = 5), matrix(runif(3*5 * 3), ncol = 3))
xy <- list(xy1, xy2, xy3)

# First find largest ID of the first data.frame.
maxid <- max(xy[[1]]$id)

# Add previous max to current ID.
for (i in 2:length(xy)) {
  xy[[i]]$id <- maxid + xy[[i]]$id
  maxid <- max(xy[[i]]$id)  # calculates largest id to be used next
}

> do.call(rbind, xy)
   id          X1          X2         X3
1   1 0.881397055 0.113236016 0.58935016
2   1 0.205762300 0.216630633 0.04096480
3   1 0.307112552 0.005092413 0.97769030
4   1 0.457299727 0.329346925 0.09582600
5   2 0.007010529 0.089751397 0.69746047
6   2 0.014806573 0.432586138 0.44480438
7   2 0.534909561 0.108258153 0.82475185
8   2 0.313796157 0.749077837 0.38798818
9   3 0.643547518 0.237040912 0.18304776
10  3 0.725906336 0.186099719 0.61738806
11  3 0.506767958 0.646870554 0.27792817
12  3 0.303638439 0.082478410 0.52484137
13  4 0.360623223 0.182054933 0.48604454
14  4 0.804174231 0.427352128 0.70075198
15  4 0.211255624 0.673377745 0.77251727
16  4 0.474358562 0.430095921 0.03648586
17  5 0.731251361 0.635859860 0.90235962
18  5 0.689463703 0.931878683 0.12179179
19  5 0.256770523 0.413928661 0.89254294
20  6 0.358319709 0.393714347 0.53143877
21  6 0.241538687 0.811901018 0.91577045
22  6 0.445141806 0.015133252 0.70977512
23  7 0.179662683 0.574578297 0.09957555
24  7 0.279302309 0.351412534 0.40911867
25  7 0.826039704 0.852739191 0.58671811
26  8 0.822024888 0.061122387 0.12308001
27  8 0.676081285 0.005285565 0.32040908
28  8 0.302821623 0.511678250 0.14814015
29  9 0.966690845 0.221078055 0.72651928
30  9 0.070768391 0.726477379 0.70431920
31  9 0.178425952 0.223096153 0.41111805
32 10 0.952963096 0.209673890 0.73485060
33 10 0.905570765 0.290359419 0.69499805
34 10 0.976600565 0.448144677 0.36100322
35 11 0.458720466 0.636912805 0.04170255
36 11 0.953471285 0.533102906 0.63543974
37 11 0.574490192 0.975327747 0.94730912
38 12 0.878968237 0.956726315 0.04761167
39 12 0.379196322 0.720179957 0.98719308
40 12 0.217246809 0.066895905 0.44981063
41 12 0.309354927 0.048701078 0.24654953
42 12 0.011187546 0.833095978 0.94793368
43 13 0.590529610 0.240967648 0.42954908
44 13 0.525187039 0.739698883 0.72047067
45 13 0.223469798 0.338660741 0.21820068
46 13 0.359939747 0.831732199 0.27095365
47 13 0.672778236 0.327900275 0.04854854
48 14 0.202447020 0.911963711 0.18576047
49 14 0.858830035 0.003633945 0.25713498
50 14 0.784197766 0.527018979 0.30911792
51 14 0.942135786 0.256841256 0.76965498
52 14 0.488395595 0.716133306 0.89618736