1
votes

If I have the code at the bottom of the post, how can I replace the column names of df1 with the second column of df2 using partial matching of df2's first column? The output should look like df3. The entirety of my data frame is filled with many other names besides .length (i.e. CS1.1.width, CS2.12.height, etc), but the CS#.#. always remains in the name.

I then need to remove the ".length" from the colnames.

I have tried using pmatch below for the first part of the question, but the output is not correct.

names(df1) <- df2$new[pmatch(names(df1), df2$partial_atch)]

How would I go about this? Thanks.

old <- c("CS1.1.length", "CS1.7.length", "CS1.10.length", "CS1.12.length", "CS2.4.length", "CS2.6.length", "CS2.9.length", "CS2.11.length", "CS1.1.height")
df1 <- data.frame()
for (k in old) df1[[k]] <- as.character()


new <- c("Bob", "Alex", "Gary", "Taylor", "Tom", "John", "Pat", "Mary")
partial_match <- c("CS1.1", "CS1.7", "CS1.10", "CS1.12", "CS2.4", "CS2.6", "CS2.9", "CS2.11")
df2 <- data.frame(Partial_Match = partial_match, Name = new)


new1 <- c("Bob.length", "Alex.length", "Gary.length", "Taylor.length", "Tom.length", "John.length", "Pat.length", "Mary.length", "Bob.height")
df3 <- data.frame()
for (k in new) df3[[k]] <- as.character()

Edit: The number of columns in df1 is greater than the number of elements in partial_match, so added an additional column in df1 as example.

1

1 Answers

0
votes

Here's an option with str_replace from the stringi package:

This works because you can use a vector of pattern = to replace with a matching replacement =.

We need to paste on the trailing . because this prevents CS1.1 replacing CS1.11 and CS1.10.

library(stringi)
stri_replace_all_regex(names(df1),
                       pattern = paste0(as.character(df2$Partial_Match),"\\."),
                       replacement = paste0(as.character(df2$Name),"\\."),
                       vectorize_all = FALSE)
#[1] "Bob.length"    "Alex.length"   "Gary.length"   "Taylor.length" "Tom.length"    "John.length"   "Pat.length"   
#[8] "Mary.length"