Omar Gonzales
Omar Gonzales

Reputation: 4008

R/Excel: Export a column with numbers in correct format to excel

Hi i've a data frame from dicember. I have a column with costs (of class num within R).

When exporting to excel, i have to put the correct format (eg. or number of money). But the problem is that in excel i see: 170997.6 - When it should be: 17,099.76.

When i apply formatting "Coin (Moneda in spanish)", it turns out: 170,997.60 (a total wrong number).

Is there a way to export this numbers in the correct format from R? Or how to do this correctly within Excel?

        date    Mes          Goal   Fuentes GoalsCompletions    ad.cost
1   2014-12-01  diciembre    Vida   Directo       0              0.00
2   2014-12-01  diciembre    Vida   Campa�as      0             0.00
3   2014-12-01  diciembre    Vida   Referencias   1              0.00
4   2014-12-01  diciembre    Vida   SEO           1              0.00
5   2014-12-01  diciembre    Vida   Email         0              0.00
6   2014-12-01  diciembre    Vida   RRSS          3              48.39
7   2014-12-01  diciembre    Vida   Adwords       3              726.70
8   2014-12-02  diciembre    Vida   Directo       0              0.00
9   2014-12-02  diciembre    Vida   Campa�as      2             36.79

Upvotes: 2

Views: 5725

Answers (1)

LyzandeR
LyzandeR

Reputation: 37879

For me the following worked (in R):

Data

(I added a few different numbers for the illustration)

df <- read.table(header=T, text='        date    Mes          Goal   Fuentes GoalsCompletions    ad.cost
1   2014-12-01  diciembre    Vida   Directo       0              0.00
2   2014-12-01  diciembre    Vida   Campanas      0             0.00
3   2014-12-01  diciembre    Vida   Referencias   1              17099.76
4   2014-12-01  diciembre    Vida   SEO           1              1233.45
5   2014-12-01  diciembre    Vida   Email         0              1000.00
6   2014-12-01  diciembre    Vida   RRSS          3              48.39
7   2014-12-01  diciembre    Vida   Adwords       3              726.70
8   2014-12-02  diciembre    Vida   Directo       0              0.00
9   2014-12-02  diciembre    Vida   Campanas      2             36.79')

#formatting with prettyNum to show as you want
df$ad.cost <- prettyNum(df$ad.cost, big.mark=',', scientific=F)

#write xlsx with the write.xlsx function of the xlsx package
library(xlsx)
write.xlsx(df, 'test.xlsx')

The output is as you want it to be (including the number you quoted in your question).

(The column in excel will be a character, which according to what you are trying to do you might need to convert back to numeric)

enter image description here

Upvotes: 1

Related Questions