17

I did a rfm analysis using package "rfm". The results are in tibble and I can't seem to figure out how to export it to .csv. I tried argument below but it exported a blank file.

> dim(bmdata4RFM)
[1] 1182580       3
> str(bmdata4RFM)
'data.frame':   1182580 obs. of  3 variables:
 $ customer_ID: num  0 0 0 0 0 0 0 0 0 0 ...
 $ sales_date : Factor w/ 366 levels "1/1/2018 0:00:00",..: 267 275 286 297 300 301 302 303 304 305 ...
 $ sales      : num  101541 110543 60932 75472 43588 ...
> head(bmdata4RFM,5)
  customer_ID        sales_date     sales
1           0 6/30/2017 0:00:00 101540.70
2           0  7/1/2017 0:00:00 110543.35
3           0  7/2/2017 0:00:00  60932.20
4           0  7/3/2017 0:00:00  75471.93
5           0  7/4/2017 0:00:00  43587.70
> library(rfm)
> # convert date from factor to date format
> bmdata4RFM[,2] <- as.Date(as.character(bmdata4RFM[,2]), format = "%m/%d/%Y") 
> rfm_result_v2
# A tibble: 535,868 x 9
   customer_id date_most_recent recency_days transaction_count    amount recency_score frequency_score monetary_score rfm_score
         <dbl> <date>                  <dbl>             <dbl>     <dbl>         <int>           <int>          <int>     <dbl>
 1           0 2018-06-30                 12               366 42462470.             5               5              5       555
 2           1 2018-06-30                 12                20     2264.             5               5              5       555
 3           2 2018-01-12                181                24     1689              3               5              5       355
 4           3 2018-05-04                 69                27     1984.             4               5              5       455
 5           6 2017-12-07                217                12      922.             2               5              5       255
 6           7 2018-01-15                178                19     1680.             3               5              5       355
 7           9 2018-01-05                188                19     2106              2               5              5       255
 8          20 2018-04-11                 92                 4      414.             4               5              5       455
 9          26 2018-02-10                152                 1       72              3               1              2       312
10          48 2017-12-20                204                 1       90              2               1              3       213
11          68 2017-09-30                285                 1       37              1               1              1       111
12          70 2017-12-17                207                 1       18              2               1              1       211
13         104 2017-08-11                335                 1       90              1               1              3       113
14         120 2017-07-27                350                 1       19              1               1              1       111
15         134 2018-01-13                180                 1      275              3               1              4       314
16         153 2018-06-24                 18                10     1677              5               5              5       555
17         155 2018-05-28                 45                 1      315              5               1              4       514
18         171 2018-06-11                 31                 6     3485.             5               5              5       555
19         172 2018-05-24                 49                 1       93              5               1              3       513
20         174 2018-06-06                 36                 3      347.             5               4              5       545
# ... with 535,858 more rows
> write.csv(rfm_result_v2,"bmdataRFMFunction_output071218v2.csv") 
3
  • 3
    That's the right way to use write.csv, are you sure you're looking in the correct directory? When you rerun the write.csv command, does the zero-length file's timestamp get updated?
    – r2evans
    Commented Jul 17, 2018 at 16:19
  • Also close the csv file before running write.csv again
    – Tung
    Commented Jul 17, 2018 at 21:31
  • Thanks for getting back. I was looking in the correct directory because the file was created. However, when I opened the file, it was blank. I also closed out the csv file prior to rerunning the argument again and was getting a blank file still.
    – slee
    Commented Jul 18, 2018 at 14:00

2 Answers 2

13

The problem seems to be that the result of the rfm_table_order is not only a tibble: looking at this question already solved, and using its data, you can know this:

> class(rfm_result)
[1] "rfm_table_order" "tibble"          "data.frame"

So if for example choose this:

> rfm_result$rfm
# A tibble: 325 x 9
   customer_id date_most_recent recency_days transaction_count amount recency_score frequency_score monetary_score rfm_score
         <int> <date>                  <dbl>             <dbl>  <int>         <int>           <int>          <int>     <dbl>
 1           1 2017-08-06                353                 1    145             4               1              2       412
 2           2 2016-10-15                648                 1    268             2               1              3       213
 3           5 2016-12-14                588                 1    119             3               1              1       311
 4           7 2017-04-27                454                 1    290             3               1              3       313
 5           8 2016-12-07                595                 3    835             2               5              5       255
 6          10 2017-07-31                359                 1    192             4               1              2       412
 7          11 2017-08-16                343                 1    278             4               1              3       413
 8          12 2017-10-14                284                 2    294             5               4              3       543
 9          15 2016-07-12                743                 1    206             2               1              2       212
10          17 2017-05-22                429                 2    405             4               4              4       444
# ... with 315 more rows

You can export it with this command:

write.table(rfm_result$rfm , file = "your_path\\df.csv")
5
  • I got it! Thank you so much. Truly appreciate!!
    – slee
    Commented Jul 30, 2018 at 18:07
  • 1
    Can you explain how do you know that "rfm" is the name of the tibble?
    – al-ash
    Commented Jun 17, 2021 at 2:49
  • @al-ash try str(rfm_result) following the code above. You'll see the strucuture of the output, and understand how to know what to fetch.
    – s__
    Commented Jun 17, 2021 at 7:17
  • what is rfm in rfm_result$rfm ? That is not clear Commented Dec 7, 2021 at 4:59
  • A tibble containing the results. look at the previous comment.
    – s__
    Commented Dec 15, 2021 at 10:36
11

OP asks for a CSV output.

Being very picky, write.table(rfm_result$rfm , file = "your_path\\df.csv") creates a TSV.

If you want a CSV add the sep="," parameter and also you'll likely want to not write out the row names so also use row.names=FALSE.

write.table(rfm_result$rfm , file = "your_path\\df.csv", sep=",", row.names=FALSE)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.