0

my code:

orders column one of new data frame by column two of previous data frame

matchOrder<- function(x,y){
    y[match(x[,2], y[,1]),];
}

orders column one of new data frame by column one of previous data frame

matchOrder1<- function(x,y){
    y[match(x[,1], y[,1]),];
}

creates the individual data frames as we want them and bind them

onea<- one[order(one[,2]),];
twoa<- matchOrder(onea,two);
threea<- matchOrder(twoa,three);
foura<- matchOrder(threea,four);
##error
fivea<- matchOrder1(foura,five);
##error
finaltable<- cbind(onea, twoa, threea, foura, fivea);
finaltable;

What I want to do is when foura throws an error, I want to make a function that will paste and cbind in everything before foura (or any error), this way I dont have to always modify the code before running it.

output:

finaltable<- cbind(onea, twoa, threea);
Chad
  • 149
  • 4
  • 11
  • This is quite hard to follow... but aren't you merging tables here? instead of sorting and cbinding, you can use the `merge` function to accomplish the same thing... – Justin Jan 29 '14 at 15:38
  • @ Justin when I merger one - five and an error is thrown in five lets say, wont that cause my merge function to throw and error? I want to bind or merge these dataframes up to table before the table that is causing an error. – Chad Jan 29 '14 at 15:42
  • 1
    Yeah, so you've merged 1,2,3 and 4. Then 5 errors so you just return your current result... (the merged first four). Also, if these are at all large, I would consider using `data.table` and its much optimized `merge`. – Justin Jan 29 '14 at 15:43
  • @ Justin, I have hundreds of dataframes, that are 250 rows in length. I know how many I will have at the end, and currently I am adding a new one everyday. I would like to automate this step, so I dont have to always go into my code and add another piece to the code so the new dataframe will be incorporated in the study. – Chad Jan 29 '14 at 15:49
  • 1
    use a list! Something like `do.call(rbind, lapply(filenames, read.csv))`. – Justin Jan 29 '14 at 16:24

0 Answers0