How to control double quotes when writing R .csv files

Sledge

My situation is that I am attempting to write a data frame consisting of columns that have differing data types to a csv file in R. The code I use to write the file is:

filename = file("equipment.csv")
write.csv(file = filename, x = equipment, quote = FALSE, row.names = FALSE ) 

This causes problems later when I try to load the csv file into a SQL database since some of the columns contain strings that contain ','. If I set quote = TRUE in the above code, it creates problems for my numeric data types when I load to the data base.

My question: is there a way to control the way that R adds quotes to the columns when writing csv files? I would like to be able to add quotes around the strings but not to the other data types.

Many thanks for your help.

Troy

Specify which columns you want quoted like this

write.csv(file = filename, x = equipment, quote = c(2,3), row.names = FALSE )

PS: if you want to automatically work out which columns to leave alone, you can do it like this:

non_numerics<-adply(1:ncol(equipment),1,function(x)print(is.numeric(equipment[,x])))
quote_val<-as.numeric(array(non_numerics[which(!non_numerics$V1),1]))

filename = file("equipment.csv")
write.csv(file = filename, x = equipment, quote = quote_val, row.names = FALSE )

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Prevent double quotes in lists of list when reading and writing csv files in pandas dataframe

How to name multiple csv files when writing data.frames to csv in R?

How to ignore double quotes when reading CSV file in Pyspark?

How to remove double quotes when loading csv into external table in impala?

How to ignore double quotes when reading CSV file in Spark?

How to remove unwanted double quotes when converting CSV to JSON in angular

Writing double quote into csv is resulting in three double quotes

How to specify quotechar in R when writing csv?

Including double quotes while writing CSV using apache commons in java

Spark dataframe not writing Double quotes into csv file properly

Read csv file in R with double quotes

How to escape double quotes inside CSV?

How to ignore double quotes from CSV file?

How to remove double quotes from a csv file

how to remove double quotes while reading CSV

How to remove double quotes(") in csv file with Python?

R - Error when writing to csv

How to change row names of a DTM when writing to .csv in R

Removing single quotes when writing dict to CSV in Python

How to remove the [1]s, [[1]]s and double quotes from a csv data in R?

How do I read in a csv with two double quotes as text qualifiers using R?

How to save character data from table/dataframe without double quotes as csv or txt file in R?

Escaping double quotes in .csv

How to use double quotes when assigning variables?

How to add quotes for awk when single and double quotes are already used?

How to save csv files with quotes escaped by slash?

How to keep quotes in CSV-Files in Python?

How to espace quotes, double quotes and colon inside double quotes when using FFMpeg

When creating a csv file, Excel shows double quotes