read.csv,标题在第一行,跳过第二行 [英] read.csv, header on first line, skip second line

查看:1485
本文介绍了read.csv,标题在第一行,跳过第二行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个标题行的CSV文件,第一行是我想要的标题,但是我想放弃第二行。如果我执行以下命令:

  data<  -  read.csv(HK Stocks bbg.csv,header = T ,stringsAsFactors = FALSE)

第一行成为头文件,第二行成为第一行我的数据框的一行:

  Xaaaaaaaaa X X.1 Xbbbbbbbbbb X.2 X.3 
1日期PX_LAST NA日期PX_LAST NA
2 31/12/2002 38.855 NA 31/12/2002 19.547 NA
3 02/01/2003 38.664 NA 02/01/2003 19.547 NA
4 03/01 / 2003 40.386不适用03/01/2003 19.547不适用
5 06/01/2003 40.386不适用06/01/2003 19.609不适用
6 07/01/2003 40.195不适用07/01/2003 19.609不适用

我想跳过此CSV文件的第二行并获得

  X1.HK.Equity X X.1 X2.HK.Equity X.2 X.3 
2 31/12/2002 38.855 NA 31/12 / 2002 19.547 NA
3 02/01/2003 38.664 NA 02/01/2003 19.547 NA
4 03/01/2 003 40.386 NA 03/01/2003 19.547 NA
5 06/01/2003 40.386 NA 06/01/2003 19.609 NA
6 07/01/2003 40.195 NA 07/01/2003 19.609 NA

我试过 data< - read.csv(HK Stocks bbg.csv ,header = T,stringsAsFactors = FALSE,skip = 1)但返回:

  Date PX_LAST X Date.1 PX_LAST.1 X.1 
1 31/12/2002 38.855 NA 31/12/2002 19.547 NA
2 02/01/2003 38.664 NA 02/01/2003 19.547 NA
3 03/01/2003 40.386 NA 03/01/2003 19.547 NA
4 06/01/2003 40.386 NA 06/01/2003 19.609 NA
5 07/01/2003 40.195 NA 07 / 01/2003 19.609 NA
6 08/01/2003 40.386 NA 08/01/2003 19.547 NA

标题行来自我CSV文件的第二行,而不是第一行。

谢谢。

  all_content = readLines(file .csv)
skip_second = all_content [-2]
dat = read.csv(textConnection(skip_second),header = TRUE,stringsAsFactors = FALSE)

使用 readLines 的第一步是将整个文件读入列表中,列表中的每个项目表示文件中的一行。接下来,使用R中的负数索引意味着选择除了此索引之外的所有数据,从而丢弃第二行。最后,我们将这些数据提供给 read.csv ,以便将它处理成 data.frame


I have a CSV file with two header rows, the first row I want to be the header, but the second row I want to discard. If I do the following command:

data <- read.csv("HK Stocks bbg.csv", header = T, stringsAsFactors = FALSE)

The first row becomes the header and the second row of the file becomes the first row of my data frame:

  Xaaaaaaaaa       X X.1     Xbbbbbbbbbb     X.2 X.3
1         Date PX_LAST  NA         Date PX_LAST  NA
2   31/12/2002  38.855  NA   31/12/2002  19.547  NA
3   02/01/2003  38.664  NA   02/01/2003  19.547  NA
4   03/01/2003  40.386  NA   03/01/2003  19.547  NA
5   06/01/2003  40.386  NA   06/01/2003  19.609  NA
6   07/01/2003  40.195  NA   07/01/2003  19.609  NA

I want to skip this second row of the CSV file and just get

  X1.HK.Equity       X X.1 X2.HK.Equity     X.2 X.3
2   31/12/2002  38.855  NA   31/12/2002  19.547  NA
3   02/01/2003  38.664  NA   02/01/2003  19.547  NA
4   03/01/2003  40.386  NA   03/01/2003  19.547  NA
5   06/01/2003  40.386  NA   06/01/2003  19.609  NA
6   07/01/2003  40.195  NA   07/01/2003  19.609  NA

I tried data <- read.csv("HK Stocks bbg.csv", header = T, stringsAsFactors = FALSE, skip = 1) but that returns:

        Date PX_LAST  X     Date.1 PX_LAST.1 X.1
1 31/12/2002  38.855 NA 31/12/2002    19.547  NA
2 02/01/2003  38.664 NA 02/01/2003    19.547  NA
3 03/01/2003  40.386 NA 03/01/2003    19.547  NA
4 06/01/2003  40.386 NA 06/01/2003    19.609  NA
5 07/01/2003  40.195 NA 07/01/2003    19.609  NA
6 08/01/2003  40.386 NA 08/01/2003    19.547  NA

The header row comes from the second line of my CSV file, not the first line.

Thank you.

解决方案

This should do the trick:

all_content = readLines("file.csv")
skip_second = all_content[-2]
dat = read.csv(textConnection(skip_second), header = TRUE, stringsAsFactors = FALSE)

The first step using readLines reads the entire file into a list, where each item in the list represents a line in the file. Next, you discard the second line using the fact that negative indexing in R means select all but this index. Finally, we feed this data to read.csv to process it into a data.frame.

这篇关于read.csv,标题在第一行,跳过第二行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆