Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

How to export NA strings to CSV using R Script?

I'm exporting country codes from Power Query through R Script using write.csv().

Problem is that I have correct text NA in dataset column which refers to country Namibia. I want to keep its semantic during export and at the same time export Nulls as empty cells to CSV/Excel. 

Unfortunately, each time R treats NA in dataset as N/A and nulls in dataset as N/A too. That's why I either get everything set to NA or, if using features like "export NA as empty string", get empty cell instead of NA (Namibia), which I want to keep in...

Please, advise a solution. It seems I have to say R somehow that it does not have to treat NA as N/A if it appears in dataset. 

PS: I also tried packages like readr, openxlsx, xlsx and so on. The result is the same. 

2 REPLIES 2
dax
Community Support
Community Support

Hi @Anonymous , 

I test this in my environment, I find that when I use write.csv directly in R, it will export data directly without missing data. When I use "write.csv(dataset,"C:\\test\\r export1.csv",na="")", it will convert NA to null, but you could add '' on NA to treat it as string.

735.PNG

Or you also could provide your simple sample data and your R script, then I will test this in my envrironment.

Please do mask sensitive data before uploading.

Thanks for your understanding and support.
Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

Anonymous
Not applicable

I have the following script

let
Source = Table.FromRecords({
[country_code = "NA", country_name = "Namibia"],
[country_code = null, country_name = "Undefined code"]
}),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"country_code", type text}, {"country_name", type text}}),
CSVWrite = R.Execute("write.csv(dataset, file = ""C:\\Temp\\write_csv_test.csv"", quote = TRUE, na="""")#(lf)output <- dataset",[dataset=#"Changed Type"]),
#"Expanded Value" = Table.ExpandTableColumn(Table.SelectColumns(CSVWrite,{"Value"}), "Value", {"country_code", "country_name"}, {"country_code", "country_name"})
in
#"Expanded Value"

Output file looks like:

"","country_code","country_name"
"1",,"Namibia"
"2",,"Undefined code"
 

"Expanded Value" also looks like:

country_code country_name
null Namibia
null Undefined code

It differs from your. I havePBi Version: 2.81.5831.821 64-bit  and R Open  3.5.3 .

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors