3

我有许多包含温度变量的表。此外,我有一个包含坐标的表格。我想将坐标表的特定行分配给温度表。换句话说,结果将在每个温度表中创建一个新列,其中包含一行坐标表。此外,根据坐标表的id,表的对应值应添加到相关的温度表中,以表名。

Table1:Tab2:Tab3: Tab4:                       
-0.52|-0.93|-0.23|-0.32                           
-0.56|-0.33|-0.13|-0.37
-0.54|-0.13|-0.33|-0.64
-0.5 |-0.93|-0.53|-0.63
-0.54|-0.83|-0.63|-0.43
-0.56|-0.53|-0.38|-0.34
-0.62|-0.03|-0.13|-0.33

Coordinate:
id       E              N       
2     2579408.2431  1079721.1499
3     2579333.7158  1079729.1852
4     2579263.6502  1079770.1125
5     2579928.0358  1080028.4605
6     2579763.6471  1079868.9218
7     2579698.0756  1079767.9762
8     2579543.1019  1079640.6512

Results:
Table2:
Temperature  coordinate 
-0.52        2579408.2431  1079721.1499                    
-0.56        2579408.2431  1079721.1499   
-0.54        2579408.2431  1079721.1499
-0.5         2579408.2431  1079721.1499
-0.54        2579408.2431  1079721.1499
-0.56        2579408.2431  1079721.1499
-0.62        2579408.2431  1079721.1499
4

1 回答 1

1
dat <- read.table(text="Table1|Table2|Table3|Table4
-0.52|-0.93|-0.23|-0.32
-0.56|-0.33|-0.13|-0.37
-0.54|-0.13|-0.33|-0.64
-0.5 |-0.93|-0.53|-0.63
-0.54|-0.83|-0.63|-0.43
-0.56|-0.53|-0.38|-0.34
-0.62|-0.03|-0.13|-0.33", sep="|", header=T)

coord <- read.table(text="id       E              N       
2     2579408.2431  1079721.1499
3     2579333.7158  1079729.1852
4     2579263.6502  1079770.1125
5     2579928.0358  1080028.4605
6     2579763.6471  1079868.9218
7     2579698.0756  1079767.9762
8     2579543.1019  1079640.6512", header=T)

首先,让我们将列名转换为数字:

> names(dat) <- gsub("[^[:digit:]]", "", names(dat))

然后堆叠数据并更改名称:

> stacked <- stack(dat)
> names(stacked) <- c("Temperature, id")

最后,我们合并数据:

> merge(stacked, coord)
   id Temperature       E       N
1   2       -0.93 2579408 1079721
2   2       -0.33 2579408 1079721
3   2       -0.13 2579408 1079721
4   2       -0.93 2579408 1079721
5   2       -0.83 2579408 1079721
6   2       -0.53 2579408 1079721
7   2       -0.03 2579408 1079721
8   3       -0.23 2579334 1079729
9   3       -0.13 2579334 1079729
10  3       -0.33 2579334 1079729
11  3       -0.53 2579334 1079729
12  3       -0.63 2579334 1079729
13  3       -0.38 2579334 1079729
14  3       -0.13 2579334 1079729
15  4       -0.32 2579264 1079770
16  4       -0.37 2579264 1079770
17  4       -0.64 2579264 1079770
18  4       -0.63 2579264 1079770
19  4       -0.43 2579264 1079770
20  4       -0.34 2579264 1079770
21  4       -0.33 2579264 1079770
于 2012-11-27T23:53:55.043 回答