0

I'm trying to merge transaction data to customer data with this code:

data <- merge(transactionData_3, customerData, all.x = TRUE)

But I'm getting this error:

Error in setnames(dt, c(by.x, start, end)) : Can't assign 11 names to a 10 column data.table

I'm not sure what it means and how to fix it. Any ideas?

str(transactionData_3)

Classes ‘data.table’ and 'data.frame':  246739 obs. of  8 variables:
 $ DATE          : Date, format: "2018-10-17" "2019-05-14" "2019-05-20" "2018-08-17" ...
 $ STORE_NBR     : int  1 1 1 2 2 4 4 5 7 7 ...
 $ LYLTY_CARD_NBR: int  1000 1307 1343 2373 2426 4149 4196 5026 7150 7215 ...
 $ TXN_ID        : int  1 348 383 974 1038 3333 3539 4525 6900 7176 ...
 $ PROD_NBR      : int  5 66 61 69 108 16 24 42 52 16 ...
 $ PROD_NAME     : chr  "Natural Chip        Compny SeaSalt175g" "CCs Nacho Cheese    175g" "Smiths Crinkle Cut  Chips Chicken 170g" "Smiths Chip Thinly  S/Cream&Onion 175g" ...
 $ PROD_QTY      : int  2 3 2 5 3 1 1 1 2 1 ...
 $ TOT_SALES     : num  6 6.3 2.9 15 13.8 5.7 3.6 3.9 7.2 5.7 ...- attr(*, ".internal.selfref")=<externalptr> 
str(customerData)

Classes ‘data.table’ and 'data.frame':  72637 obs. of  3 variables:
 $ LYLTY_CARD_NBR  : int  1000 1002 1003 1004 1005 1007 1009 1010 1011 1012 ...
 $ LIFESTAGE       : chr  "YOUNG SINGLES/COUPLES" "YOUNG SINGLES/COUPLES" "YOUNG FAMILIES" "OLDER SINGLES/COUPLES" ...
 $ PREMIUM_CUSTOMER: chr  "Premium" "Mainstream" "Budget" "Mainstream" ...- attr(*, ".internal.selfref")=<externalptr>
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 4
    We can't help if we don't know what your data are. Please provide a sample of your data by editing your question and adding the output of `dput(data)` for both data frames. Also review the information here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – John Polo Feb 15 '23 at 01:04
  • @JohnPolo the output of dput(data) is very large. Do you know how to just get a sample of this? – Anita Abbot Feb 15 '23 at 02:48
  • @AnitaAbbot you can use `dput` on a subset of data – akrun Feb 15 '23 at 02:49
  • @akrun It won't let me post the output in the post - I keep getting this error message "Your post appears to contain code that is not properly formatted as code." Any ideas? – Anita Abbot Feb 15 '23 at 03:13
  • Can you update with the `str(transactionData_3)` and `str(customerData)` – akrun Feb 15 '23 at 03:13
  • @akrun please let me know if this is what you were after. Thanks so much! – Anita Abbot Feb 15 '23 at 03:20
  • Can you specify the `by` i..e `by = "LYLTY_CARD_NBR"` in `merge`. The error seems weird to me. Can you try the `merge` again on a fresh R session with only data.table loaded – akrun Feb 15 '23 at 03:24

0 Answers0