2

I'm getting some location info from Twitter and I can't get the foreign language characters to come out right when I convert the list into data frame. For example, I have this code:

x <- rbind('МоскваРоссия','knoxfieldmelbourne', 'CA US','MelbrneAustralia')
y <- data.frame(rbind('МоскваРоссия','knoxfieldmelbourne', 'CA US','MelbrneAustralia'))

write.csv(x,'\\test2.csv')

If I just read out 'x' in the console I get:

> x
     [,1]                
[1,] "МоскваРоссия"      
[2,] "knoxfieldmelbourne"
[3,] "CA US"             
[4,] "MelbrneAustralia"  

But when I write X into csv or when I look at it in viewer I get: enter image description here

I'm running on Windows and using Excel to open CSV file.

If I run 'y' in the console I get the same result as above. Obviously R can read non-English characters but why does it change when I copy it to csv?

Cœur
  • 37,241
  • 25
  • 195
  • 267
jmich738
  • 1,565
  • 3
  • 24
  • 41
  • What OS are you using (Windows does not use UTF-8 by default)? What "viewer" are you using? What are you using to look at the csv file with? – MrFlick May 26 '15 at 02:33
  • I've edited my post. The viewer is R Studio viewer. I'll keep trying to use the code from the post mentioned by @Pascal but its not working for me at the moment. – jmich738 May 26 '15 at 02:36
  • If you really need excel, try opening the csv using Google Spreadsheet and then exporting to xlsx. More discussion here: http://stackoverflow.com/questions/4221176/excel-to-csv-with-utf8-encoding – Will Cornwell May 26 '15 at 07:50

1 Answers1

1

Those are Russian characters. Unless you're working with a lot of different languages and don't know which is which, simply changing the R locale should work. Also, the file encoding needs to be UTF.

Sys.setlocale(locale = "Russian")
write.csv(x,'\\test2.csv',fileEncoding = "UTF-8")