A.Amidi
A.Amidi

Reputation: 2522

Adding a specific Row of a Table to the number of tables In R with respect to the Table name

I have number of tables which contain temperature variable. Moreover I have a table which contain coordinates. I want to assign a specific row of the coordinate table to the temperature tables. In other words as a result a new column will be created in each temperature table which contains a row of the coordinate table. Moreover, based on id of the coordinate table corresponding values of the table should be added to the relevant temperature table with respect to the table name.

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

Upvotes: 3

Views: 161

Answers (1)

sebastian-c
sebastian-c

Reputation: 15395

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)

First, let's turn the column names into digits:

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

Then stack the data and change the names:

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

Finally, we merge the data:

> 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

Upvotes: 1

Related Questions