Reputation: 233
I have a data.frame
like this:
dsl.0 dsl.10 dsl.2 dsl.11 dsl.1 dsl.3 dsl.5 dsl.4 dsl.6 dsl.7 dsl.9 dsl.8 dsl.14 dsl.13 dsl.12
1 0 0 0 161 1 0 0 173 1 1 0 0 0 165 3
2 0 0 0 161 161 133 0 173 1 1 0 0 0 165 3
3 0 0 0 161 161 133 0 173 1 1 0 0 0 165 3
4 0 0 0 161 161 133 0 173 1 1 0 0 0 165 3
5 0 0 0 161 161 133 0 173 1 1 0 0 0 165 3
6 0 0 0 161 161 133 0 173 1 1 0 0 0 165 3
The names of columns are comprised of dsl.
and N
, where N
is index. The names make no sense, so I want to replace them by their real name.
The relation between names and index of dsl
is as following:
local_index local_name threshold sg_local_index sg_local_name location_id
1 0 A40_1 84 0 A 4
2 10 X201 40 0 A 4
4 2 X501 40 0 A 4
5 11 X202 84 0 A 4
8 1 A40_2 84 0 A 4
12 3 X502 40 0 A 4
23 5 D1 40 3 D 4
27 4 D50 84 3 D 4
28 6 E50 84 4 E 4
29 7 E1 40 4 E 4
39 9 K60_2 84 9 K 4
40 8 K60_1 84 9 K 4
41 14 L0_2 84 10 L 4
42 13 L50 84 10 L 4
46 12 L0_1 40 10 L 4
local_index
are represented by dsl.N
in the first data.frame
; I want to change the names of columns in first data.frame
from dsl.N
to the real name, like A40_1,X202
. Is it possible to deal with the names of columns like that?
Upvotes: 1
Views: 73
Reputation: 886938
Try (if d1
and d2
are the two datasets)
names(d1) <- d2$local_name[match(as.numeric(gsub(".*\\.", "",colnames(d1))),
d2$local_index)]
head(d1,2)
# A40_1 X201 X501 X202 A40_2 X502 D1 D50 E50 E1 K60_2 K60_1 L0_2 L50 L0_1
#1 0 0 0 161 1 0 0 173 1 1 0 0 0 165 3
#2 0 0 0 161 161 133 0 173 1 1 0 0 0 165 3
Upvotes: 3