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,]))