簡體   English   中英

來自csv的R中的2行標題

[英]2 lines of headers in R from csv

我有很多帶有雙標頭的csv文件,如下所示。 (這只是其中的一部分,並且兩個標頭都包含重要信息)如何合並csv文件的前兩行以獲得單行標頭? (egLife.expectancy.at.birth..years..1Female)

  Life.expectancy.at.birth..years..1 Life.expectancy.at.birth..years..2
1                             Female                               Male
2                                 62                                 61
3                                 61                                 58
4                                 56                                 54
5                                 50                                 49
6                                 76                                 73

閱讀兩次,然后將標題粘貼在一起。 對於第二個讀取限制,因為我們實際上只需要標頭,所以讀取的行數有限。

# in next 2 lines replace text=Lines with something like "myfile"
DF <- read.table(text = Lines, header = TRUE, skip = 1)
hdr1 <- read.table(text = Lines, header = TRUE, nrows = 1)
names(DF) <- paste0(names(hdr1), names(DF))

贈送:

> DF
  Life.expectancy.at.birth..years..1Female Life.expectancy.at.birth..years..2Male
1                                       62                                     61
2                                       61                                     58
3                                       56                                     54
4                                       50                                     49
5                                       76                                     73

注意:我們將其用於輸入Lines

Lines <- "  Life.expectancy.at.birth..years..1 Life.expectancy.at.birth..years..2
                             Female                               Male
                                 62                                 61
                                 61                                 58
                                 56                                 54
                                 50                                 49
                                 76                                 73"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM