2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Rでのファイル操作/文字化けの対策をしたい/CSVファイル

Last updated at Posted at 2021-11-29

ファイルの読み書きをする

CSVファイルの読み書き

読む

read_csv("ファイル.csv")

書く

ここでは使用中のディレクトリが自動で保存先となる。
_row.names = FALSE_を書くと、一番左の列に数字が入ることを防ぐことが出来る。

write.csv(df,"df.csv",row.names = FALSE)

文字化け対策

しかしファイルには文字コードというものがあり、それが違うファイルを開くと文字化けしてしまう。
Windowsz→Shift-jis
mac→UTF-8
が使われていることが多い。

読みでは元の文字コード、書きでは書きたい文字コードを指定することで文字化けを防ぐことが出来る。

読む

read_csv("data/data_utf8.csv", locale = locale(encoding = "utf8"))

書く

write.csv(df,"df.csv",fileEncoding="CP932",row.names = FALSE)
# CP932はShift-Jisのことを指している。
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?