在R中读取两行标题 [英] Reading two-line headers in R
问题描述
这种情况一直发生在我身上,就像人们一样通常使用一行作为列名称,然后在其下面包含另一行作为度量单位。我不想跳过任何东西。我想要名称和单位通过。
这是什么具有两个头文件的典型文件可能看起来像:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ t $生物质产量
作物Mg /公顷
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
我会做两个步骤,假设我们知道第一行包含标签,并且总是有两个标头。
标题< - scan(file.txt,nlines = 1,what = character())
data < - read.table(file.txt ,skip = 2,header = FALSE)
然后添加字符向量头
作为名称
组件ent:
names(data)< - header
$ p $对于您的数据,这将是
头< - 扫描( data.txt,nlines = 1,what = character())
data < - read.table(data.txt,skip = 2,header = FALSE)
名称(数据)< ; - header
head(data)
> (数据)
trt生物量收益
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
CCW 17.45 190.59
如果你想要单位,按照@Dinin的答案,然后在第二行做第二个
scan()
header2<扫描(data.txt,skip = 1,nlines = 1,what = character())
名称(数据)< - paste0(header,header2)
> (数据)
trtcrop生物量mg / 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
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.
Here is what a typical file with two headers might look like:
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)
Then add the character vector
header
on as thenames
component:names(data) <- header
For your data this would be
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
If you want the units, as per @DWin's answer, then do a second
scan()
on line 2header2 <- 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屋!