在 R 中读取两行标题 [英] Reading two-line headers in R
问题描述
当标题有两个必要的标题行时,将文件读入 R 的最佳方法是什么?
What is the best way to read a file into R when the header has two necessary lines for the header?
这种情况一直发生在我身上,因为人们经常使用一行作为列名,然后在其下方包含另一行作为度量单位.我不想跳过任何东西.我想要名字和单位.
This happens to me all the time, as people often use one line for the column name and then include another line underneath it for the unit of measurement. I don't want to skip anything. I want the names and the units to carry through.
trt biomass yield
crop Mg/ha bu/ac
C2 17.76 205.92
C2 17.96 207.86
CC 17.72 197.22
CC 18.42 205.20
CCW 18.15 200.51
CCW 17.45 190.59
P 3.09 0.00
P 3.34 0.00
S2 5.13 49.68
S2 5.36 49.72
推荐答案
我会做两个步骤,假设我们知道第一行包含标签,并且总是有两个标题.
I would do two steps, assuming we know that the first row contains the labels, and there are always two headers.
header <- scan("file.txt", nlines = 1, what = character())
data <- read.table("file.txt", skip = 2, header = FALSE)
然后添加字符向量 header
作为 names
组件:
Then add the character vector header
on as the names
component:
names(data) <- header
对于您的数据,这将是
header <- scan("data.txt", nlines = 1, what = character())
data <- read.table("data.txt", skip = 2, header = FALSE)
names(data) <- header
head(data)
> head(data)
trt biomass yield
1 C2 17.76 205.92
2 C2 17.96 207.86
3 CC 17.72 197.22
4 CC 18.42 205.20
5 CCW 18.15 200.51
6 CCW 17.45 190.59
如果你想要单位,按照@DWin 的回答,然后在第 2 行做第二个 scan()
If you want the units, as per @DWin's answer, then do a second scan()
on line 2
header2 <- scan("data.txt", skip = 1, nlines = 1, what = character())
names(data) <- paste0(header, header2)
> head(data)
trtcrop biomassMg/ha yieldbu/ac
1 C2 17.76 205.92
2 C2 17.96 207.86
3 CC 17.72 197.22
4 CC 18.42 205.20
5 CCW 18.15 200.51
6 CCW 17.45 190.59
这篇关于在 R 中读取两行标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!