Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export tibble to .csv

Tags:

r

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") 
like image 465
slee Avatar asked Jul 17 '18 16:07

slee


People also ask

Is RFM_table_order a Tibble or a CSV?

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: Show activity on this post. OP asks for a CSV output.

How to export data from R to CSV file?

Set the destination path. Path + filename + extension i.e. "/Users/USERNAME/Downloads/mydata.csv" or the filename + extension if the folder is the same as the working directory Note: You can use the function write.csv in R as write.csv2 () to separate the rows with a semicolon for R export to csv data.

How to write a matrix instead of a Tibble?

It looks like you have a matrix instead of a tibble. In any case, you can use the write.xlsx function of the openxlsx package. Also, you can round the values of the matrix with the round () function.

How to save and export data from xlsx?

Just as the windows users, you can save data with the function write.xlsx () Exporting data to different software is as simple as importing them. The library “haven” provides a convenient way to export data to First of all, import the library.


2 Answers

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'l 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)
like image 162
jacanterbury Avatar answered Oct 20 '22 02:10

jacanterbury


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")
like image 38
s__ Avatar answered Oct 20 '22 03:10

s__