
3行目をcolnamesにする前に "for" を使って名前を順に変更していくことにした。
# 3行目のIDを変更
for(i in 3:6){
Dat1[3,i] <- paste("M_NFT_",c(i-2))
}
for(i in 7:10){
at1[3,i] <- paste("M_DN_",c(i-6))
}
for(i in 11:14){
Dat1[3,i] <- paste("F_NFT_",c(i-10))
}
for(i in 15:18){
Dat1[3,i] <- paste("NFT_",c(i-14))
}
ID変更前↓

ID変更後↓

で、この3行目を "colnames" に指定し、要らなくなった 1-3行目を削除。
# 3行目を "colnames" に指定
colnames(Dat1) <- Dat1[3,]
# 1-3行目を削除
Dat1 <- Dat1[-(1:3),]
無事に "colnames" を目的の形に変更できた。

で、str() で それぞれのデータ型を見ると、すべて文字列になっている。

これを "numeric" に変換するために下記コードを入力。
# numeric に変換
for(i in 3:18){
Dat1[,i] <- as.numeric(Dat1[,i])
}
すると、下記エラーメッセージが出現した。
Error in as.numeric(Dat1[, i]) :
'list' object cannot be coerced to type 'double'
これはどーしたものかとググると、下記に答えが。
This tutorial explains how to fix the following error in R: (list) object cannot be coerced to type 'double'.
どうやら、私がリストタイプをnumericに変換しようとしてるからエラーが起こっているらしい。
で、これを解決するためには、
unlist()
をつけてリストを解除すれば良い様子。
# listを解除してnumericに変換
for(i in 3:18){
Dat1[,i] <- as.numeric(unlist(Dat1[,i]))
}
無事 "numeric" に変換できた。
