r - read.csv - 跳过具有不同列数的行

dat*_*dan 7 csv null r skip

我的csv文件顶部有5行作为有关该文件的信息,我不需要这些行.

这些信息行只有2列,而标题和数据行(来自6个on-wards)有8个.这似乎是问题的原因.

我尝试使用read.csv中的skip函数跳过这些行,read.table也是如此

df = read.csv("myfile.csv", skip=5)
df = read.table("myfile.csv", skip=5)
Run Code Online (Sandbox Code Playgroud)

但这仍然给我相同的错误信息,即:

Error in read.table("myfile.csv",  :empty beginning of file
Run Code Online (Sandbox Code Playgroud)

另外:警告信息:

1: In readLines(file, skip) : line 1 appears to contain an embedded nul
2: In readLines(file, skip) : line 2 appears to contain an embedded nul
...
5: In readLines(file, skip) : line 5 appears to contain an embedded nul
Run Code Online (Sandbox Code Playgroud)

如何在没有前5行中的空值的情况下将此.csv读入r中导致此问题?

jba*_*ums 6

你可以尝试:

read.csv(text=readLines('myfile.csv')[-(1:5)])
Run Code Online (Sandbox Code Playgroud)

这将首先将每一行存储在其自己的向量元素中,然后删除前五行并将其余部分视为csv.

  • 这些天不需要`textConnection()`.你可以使用`read.csv(text = readLines(...))`. (2认同)