R 无法更改数据框列名
我有下面的数据框R 无法更改数据框列名,r,R,我有下面的数据框 > head(weather) V6 V7 V8 V9 V10 V11 1 2 3 4 Day Hour Value Unit Duration QC
> head(weather)
V6 V7 V8 V9 V10 V11
1
2
3
4 Day Hour Value Unit Duration QC Name
5 1 0 231 \xb5g/m\xb3 1 Hr Valid
6 1 1 239 \xb5g/m\xb3 1 Hr Valid
前三行为空,列名为V6
至V11
。我想用第四行作为列名。我试过下面的方法,但不起作用
colnames(weather) <- weather[4,]
我不知道这些值是从哪里来的。如何更新数据框中的列名?错误是由于列的类别造成的。当它被强制到
整数
存储模式时,它可能是因子
。通过使用skip
参数读取,可以在其根目录中更正此问题
weather <- read.csv("yourfile.csv", skip=4, header=TRUE, stringsAsFactors=FALSE)
weather <- read.csv("yourfile.csv", skip=4, header=TRUE, stringsAsFactors=FALSE)
colnames(weather) <- as.character(unlist(weather[4,]))