Maxwell Chandler
Maxwell Chandler

Reputation: 642

Dynamic column rename based on a separate data frame in R

Generate df1 and df2 like this

pro <- c("Hide-Away", "Hide-Away")
sourceName <- c("New Rate2", "FST")
standardName <- c("New Rate", "SFT") 
df1 <- data.frame(pro, sourceName, standardName, stringsAsFactors = F)
A <- 1; B <- 2; C <-3; D <- 4; G <- 5; H <- 6; E <-7; FST <-8; Z <-8
df2<- data.frame(A,B,C,D,G,H,E,FST)
colnames(df2)[1]<- "New Rate2"

Then run this code.

df1 <- df1[,c(2,3)]
index<-which(colnames(df2) %in% df1[,1])
index2<-which(df1[,1] %in% colnames(df2) )
colnames(df2)[index] <- df1[index2,2]

The input of DF2 will be like

  New Rate2 B C D G H E FST
          1 2 3 4 5 6 7   8

The output of DF2 will be like

  New Rate B C D G H E SFT
         1 2 3 4 5 6 7   8

So clearly the code worked and swapped the names correctly. But now create df2 with the below code instead. And make sure to regenrate df1 to what it was before.

df2<- data.frame(FST,B,C,D,G,H,E,Z)
colnames(df2)[8]<- "New Rate2"

and then run

df1 <- df1[,c(2,3)]
index<-which(colnames(df2) %in% df1[,1])
index2<-which(df1[,1] %in% colnames(df2) )
colnames(df2)[index] <- df1[index2,2]

The input of df2 will be

 FST B C D G H E New Rate2
   8 2 3 4 5 6 7         8

The output of df2 will be

New Rate B C D G H E SFT
       8 2 3 4 5 6 7   8

So the order of the columns has not been preserved. I know this is because of the %in code but I am not sure of an easy fix to make the column swapping more dynamic.

Upvotes: 0

Views: 42

Answers (2)

user1533380
user1533380

Reputation:

Trying to follow your example, in your second pass I get an index value of 0,

> df2
  New Rate B C D G H E SFT
1        8 2 3 4 5 6 7   8
> df1
  sourceName standardName
1  New Rate2     New Rate
2        FST          SFT
> index<-which(colnames(df2) %in% df1[,1])
> index
integer(0)

which would account for your expected ordering on assignment to column names.

Upvotes: 0

be_green
be_green

Reputation: 753

I am not totally sure about the question, as it seems a little vague. I'll try my best though--the best way I know to dynamically set column names is setnames from the data.table package. So let's say that I have a set of source names and a set of standard names, and I want to swap the source for the standard (which I take to be the question).

Given the data above, I have a data.frame structured like so:

> df2
  A B C D G H E FST
1 1 2 3 4 5 6 7   8

as well as two vectors, sourceName and standardName.

sourceName <- c("A", "FST")
standardName <- c("New A", "FST 2: Electric Boogaloo") 

I want to dynamically swap sourceName for standardName, and I can do this with setnames like so:

df3 <- as.data.table(df2)
setnames(df3, sourceName, standardName)

> df3
   New A B C D G H E FST 2: Electric Boogaloo
1:     1 2 3 4 5 6 7                        8

Upvotes: 1

Related Questions