0

I am using R to analyze some text data. After doing some aggregation, I had a new dataframe I wanted to write to a csv file, so I can use it in other analyses. The dataframe looks correct in r- it only has 2 columns with text data- but once I write the csv and open it, the text is scattered across different columns. Here is the code I was using:

write.csv(new_df, "4.19 Group 1_agg by user try 2.csv")

I tried adding in an extra bit of code to specify that it should be using UTF-8, since I've heard this could be an encoding error, so the code then looked like this:

write.csv(new_df, "4.19 Group 1_agg by user try 2.csv", fileEncoding = "UTF-8")

I also tried reading in the file differently (using fread instead of read.csv)

Still, the csv file looks wrong/messy in many places. Here is what it should look like:

enter image description here

This is what it looks like currently:

enter image description here

Again, I think the error must be in writing the csv file, because everything looks good in R when I check it using names and head. Any help is appreciated, thank you!

Phil
  • 7,287
  • 3
  • 36
  • 66
Rachel
  • 1
  • 1
  • Hi and welcome on SO. First keep in mind that providing a reproducible example is a good practice for future questions. See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Second point, I think this view is due to Excell or the software you open your csv with. I think this is purely graphic and adding a third column with a random string will restrain the second column graphically. – Gowachin Apr 19 '21 at 20:56
  • Did you try to specify `sep=','` ? – Taher A. Ghaleb Apr 20 '21 at 04:16
  • As suggested by Taher, with sep="," you point to the comma separated version of csv. In Europe the csv2 format with ";" is frequently used. You are also using many "special" characters that can make it difficult for the interpreter to open the file in the correct format (including table cell assignment). – Ray Apr 20 '21 at 08:49

0 Answers0