|
DATA MINING
Desktop Survival Guide by Graham Williams |
|
|||
Simple Linking |
The merge function can be used to join several datasets on common fields. the default behaviour is to join on any columns that the data frames have in common. This is what we demonstrate below.
> ds1 <- read.table(file("clipboard"), header=T)
> ds1
id age gender
1 1 32 M
2 2 45 F
3 3 29 F
> ds2 <- read.table(file("clipboard"), header=T)
> ds2
id day x1
1 1 1 0.52
2 1 2 0.72
3 1 3 0.29
4 2 1 0.51
5 2 2 0.18
6 3 2 0.22
7 3 3 0.54
> ds3 <- read.table(file("clipboard"), header=T)
> ds3
id day x2
1 1 1 0.34
2 1 2 0.55
3 1 3 0.79
4 2 1 0.12
5 2 2 0.23
6 3 2 0.45
7 3 3 0.56
> merge(ds1, ds2)
id age gender day x1
1 1 32 M 1 0.52
2 1 32 M 2 0.72
3 1 32 M 3 0.29
4 2 45 F 1 0.51
5 2 45 F 2 0.18
6 3 29 F 2 0.22
7 3 29 F 3 0.54
> merge(merge(ds1, ds2), ds3)
id day age gender x1 x2
1 1 1 32 M 0.52 0.34
2 1 2 32 M 0.72 0.55
3 1 3 32 M 0.29 0.79
4 2 1 45 F 0.51 0.12
5 2 2 45 F 0.18 0.23
6 3 2 29 F 0.22 0.45
7 3 3 29 F 0.54 0.56
|