Achal Neupane
Achal Neupane

Reputation: 5729

Replace column names with the string that partially match in R

I have a dataframe with column names mycolumns (have more than 2000 columns). I have this obect called myobject which contains sets of strings that partially matches with the column names(each matches with only one column name) in mycolumns. I want to replace the column names with the respective strings in my object.So the new column names of the dataframe will be "jackal","cat.11","Rat.Fox". Please note this has to be done by using pattern matching or regex as the order of the matched names could be different in myobject.

mycolumns <- c("jackal.fox11.FAD", "cat.11.miss.DAD", "Rat.Fox.11.33.DDG")
myobject  <- c("jackal","Rat.Fox","cat.11")

Upvotes: 1

Views: 1720

Answers (3)

hoffmanc
hoffmanc

Reputation: 622

Data setup:

> mycols = qw("jackal.fox11.FAD cat.11.miss.DAD Rat.Fox.11.33.DDG")
> df = read.csv(textConnection("1,2,3"), header=F)
> names(df) = qw("jackal Rat.Fox cat.11")

The business:

> names(df) = sapply(names(df), function(n) mycols[grepl(n, mycols)])

The result:

> names(df)
[1] "jackal.fox11.FAD"  "Rat.Fox.11.33.DDG" "cat.11.miss.DAD"

props to @luke-singham for basis of approach

qw defined in my .Rprofile as in https://stackoverflow.com/a/31932661/338303

Upvotes: 2

Luke Singham
Luke Singham

Reputation: 1736

How about a for loop with grep:

 #your example
 mycolumns <- c("jackal.fox11.FAD", "cat.11.miss.DAD", "Rat.Fox.11.33.DDG")
 myobject  <- c("jackal","Rat.Fox","cat.11")

 #for loop solution
 for(i in myobject){
      mycolumns[grepl(i, mycolumns)] <- i
 }

Upvotes: 2

alexwhitworth
alexwhitworth

Reputation: 4907

If you can guarantee that the names are the same as here, this is quite simple. However, that situation is trivial, so there doesn't seem to be any value in the solution vs just names(df) <- myobject

names(df)[c(grep(myobject[1], mycolumns), grep(myobject[2], mycolumns), grep(myobject[3], mycolumns))] <- myobject

Upvotes: 0

Related Questions