LoginSignup
0
0

Rでカラム名の文字化けを直す方法

Last updated at Posted at 2023-03-04

症状

『現場ですぐ使える時系列データ分析』の第五章で、データフレームからデンドログラムを作成する際に文字化けが発生した。
どうやらカラムの日本語にShift_JISが使用されてるとのこと。

やってみたこと

文字コードのデフォルトを.Rprofileを使ってShift_JISにしてみる

.Rprofile
options(encoding="Shift_JIS")

文字コード確認

getOption("encoding")
[1] "Shift_JIS"

ちゃんと変化はしている。

しかし文字化けは相変わらず……

Shift_JIS、Shift-jis、CP932、あるいはUTF-8など試してみたものの、特に変化なし。
この設定では意味がないことはわかった。

解決方法

以下の方法で文字コードを変換し、データフレームを書き換える。

names(data.log.core30) <- iconv(names(data.log.core30), from = "shift-jis", to = "utf8")

# デンドログラムの表示
fit.clust=hclust(dist(t(data.log.core30)))
plot(fit.clust)

image.png

参考にさせていただいた記事

MacでRStudioを使うときのドツボリメモ
[小ネタ]Rでデータフレームのカラム名の文字コードを変換する

0
0
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
0
0