在利用R语言读取csv文件时,第一列列名总是出现乱码,代码如下:
setwd(“E:\2.Model\4. Simulation”) #设定文件路径
All.Soils = read.table(“1. Soil.csv”,sep=",",header=T) #读取文件
xx<-colnames(All.Soils)#获取文件中的列名
xx
结果如下:
[1] “锘緾lass” “FID” “BD1” “BD2” “BD3”
[6] “BD4” “BD5” “airdry1” “airdry2” “airdry3”
[11] “airdry4” “airdry5” “ll15_1” “ll15_2” “ll15_3”
[16] “ll15_4” “ll15_5” “CLL1” “CLL2” “CLL3”
[21] “CLL4” “CLL5” “dul1” “dul2” “dul3”
[26] “dul4” “dul5” “sat1” “sat2” “sat3”
[31] “sat4” “sat5”
后来找到了原因,是因为将excel文件另存为csv文件造成的。
应该在excl中按照如下操作完成:
第一步:点击“file”,即“文件”;
第二步:点击“export”,即“导出”;
第三步:点击“Change File Type”,即“改变文件格式”,选择"CSV",进行导出即可;
之后再次运行代码,乱码不见了,结果如下:
[1] “Class” “FID” “BD1” “BD2” “BD3” “BD4”
[7] “BD5” “airdry1” “airdry2” “airdry3” “airdry4” “airdry5”
[13] “ll15_1” “ll15_2” “ll15_3” “ll15_4” “ll15_5” “CLL1”
[19] “CLL2” “CLL3” “CLL4” “CLL5” “dul1” “dul2”
[25] “dul3” “dul4” “dul5” “sat1” “sat2” “sat3”
[31] “sat4” “sat5”
如果觉得《R语言读取csv文件 第一列列名出现乱码的解决方法》对你有帮助,请点赞、收藏,并留下你的观点哦!