new2argh
new2argh

Reputation: 1

change column name based on a match to another data frame

I have a df (foo) with subject characteristics then some genomic data (in the form of enzyme commission numbers or ECs if that is important).

sample  ca_avg  id  age 1.1.1.1     1.1.1.100   1.1.1.12
124-5   1003    124 80  0           0.0001      0.654

I would like to change the enzyme commission numbers to their actual names. I imported a .txt to a df (enames) which holds the key to convert ECs -> names with a file that has more ECs than are actually in foo.

ec_num      1.1.1.1 1.1.1.100   1.1.1.102   1.1.1.103   1.1.1.108
ec_name     adh     oacp        3dr         lt3h        c3d

I would like to replace foo's column name with the ec_name (if available), otherwise, leave the colname alone like so:

sample  ca_avg  id  age adh oacp    1.1.1.12
124-5   1003    124 80  0   0.0001  0.654

The first thing I tried was to find the intersect between the dataframes:

common_col <- intersect(colnames(foo), colnames(enames))

I then tried to do many iterations of

if (colnames(foo) %in% common_col){ colnames(foo) <- colnames(enames)}

but it isn't working.

Anything I try after this for conditional renaming fails. Any help would be greatly appreciated.

Upvotes: 0

Views: 1527

Answers (1)

AntoniosK
AntoniosK

Reputation: 16121

This should help:

# example datasets
foo = data.frame(x=1,
                 y=2,
                 z=3)

enames = data.frame(y = "YY",
                    z = "ZZ", stringsAsFactors = F)

# see foo
foo

#   x y z
# 1 1 2 3

# keep common column names
common_col = intersect(names(foo), names(enames))

# replace common column names in foo with corresponding values from enames
names(foo)[names(foo) %in% common_col] = enames[common_col]

# check foo again
foo

#   x YY ZZ
# 1 1  2  3

You can also use colnames instead of names if you prefer.

Upvotes: 1

Related Questions