star star - 2 months ago 7
R Question

How can select similar columns and remove others?

I have two data frame as below. I would like to change colnames of Data 2 based on first Column of Data 1, and if the colnames of Data 2 is simialr to Data 1$ A , put the name of Dat 1 $ B for colnames of Data 2, otherwise unsimilar columns ignored.

Data 1:

A B
2-32 AD
3-45 AC
5-44 BC


Data 2:

RS 2-32 5-66 5-44 6-11 3-45
rs34 3 5 6 7 5
rs45 4 3 3 4 5


Output:

RS AD BC AC
rs34 3 6 5
rs45 4 3 5


I have used 'ifelse' command but I could not remove unsimilar columns.

a<- ifelse (colnames(Data2) %in% Data1 $ A, Dat1 $ B , '')

Answer

You could do:

names(df2)[-1] <- df1[match(names(df2)[-1], df1$A),]$B
df2 <- df2[,!is.na(names(df2))]

#    RS AD BC AC
#1 rs34  3  6  5
#2 rs45  4  3  5