Several things in your question make it very difficult to help you:
- You did not use the code-formatting button, so your code is very hard to read.
- We do not have access to your data files, so we can't understand specifically what might be causing your error.
- You don't mention the actual error message you are getting, or whether you simply get a result that you don't think makes sense.
The best thing for getting a rapid answer would be if you could create a small dataset that causes the error you are seeing, and post code with real data, without making us guess at the possible contents of your data files.
