change column name based on a match to another data frame
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))
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)}
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.
1 Answer
1
# example datasets
df1 = data.frame(x=1,
y=2,
z=3)
df2 = data.frame(y = "Y",
z = "Z", stringsAsFactors = F)
# see df1
df1
# x y z
# 1 1 2 3
# keep common column names
common_col = intersect(names(df1), names(df2))
# replace common column names in df1 with corresponding values from df2
names(df1)[names(df1) %in% common_col] = df2[common_col]
# check df1 again
df1
# x Y Z
# 1 1 2 3
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment