问题描述
在从 csv 文件读入数据框之前,我使用 read.csv 中的跳过选项跳过几行.但是,当我在执行此操作时执行名称(数据框)时,我会丢失列名并获得一些随机字符串作为列名.为什么会发生这种情况?
I was using the skip option in read.csv to skip a few lines before reading into my data frame from a csv file. However, when I do a names(dataframe) upon doing this, I lose my column names and get some random strings as column names. Why does this happen?
> mydf = read.csv("mycsvfile.csv",skip=100)
> names(mydf)
[1] "X2297256" "X3"
没有跳过选项,它工作正常
Without the skip option, it works fine
> mydf = read.csv("mycsvfile.csv")
> names(mydf)
[1] "col1" "col2"
推荐答案
如果你跳过文件中的行,你会跳过整行,所以如果你的标题在第一行并且你跳过了 100 行,标题行将被跳过.如果您想跳过文件的一部分并仍然保留标题,则需要单独阅读它们
If you skip lines in a file, you skip the complete line, so if your header is in the first line and you skip 100 lines, the header line will be skipped. If you want to skip part of the the file and still keep headers, you'll need to read them separately
headers <- names(read.csv("mycsvfile.csv",nrows=1))
mydf <- read.csv("mycsvfile.csv", header=F, col.names=headers, skip=100)
这篇关于与 read.csv 一起使用跳过时无法获取列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!