Home > Mobile >  How to overlap variables names to colnames in 2 different dataframe?
How to overlap variables names to colnames in 2 different dataframe?

Time:07-21

How to overlap variables names to colnames in 2 different dataframe? Here is the first dataframe

data <- data.frame(c('AA','BB','CC','DD','EE'),
                 c(1,0,1,2,0),
                c(1,0,1,2,0),
               c(2,0.5,1,2,0),
                c(1,2,1,3,0))


colnames(data) <- c('name', 'A1', 'A2', 'A3', 'A4')

data
#  name A1 A2  A3 A4
#1   AA  1  1 2.0  1
#2   BB  0  0 0.5  2
#3   CC  1  1 1.0  1
#4   DD  2  2 2.0  3
#5   EE  0  0 0.0  0

And here is 2nd dataframe

data2 <- data.frame(c('A2','A4','A6','A7'),
                c(1,0,1,2))
colnames(data2) <- c('ID', 'num')
data2
#   ID num
# 1 A2   1
# 2 A4   0
# 3 A6   1
# 4 A7   2

After overlapping data2$ID to colnames(data) here is my desired output.

    name  A2  A4
#1   AA    1   1
#2   BB    0   2
#3   CC    1   1
#4   DD    2   3
#5   EE    0   0

Tidyverse approach would be helpful. Thank you.

CodePudding user response:

Maybe you want something like this where you subset the columns based on elements in your ID column including the name column of data:

data <- data.frame(c('AA','BB','CC','DD','EE'),
                   c(1,0,1,2,0),
                   c(1,0,1,2,0),
                   c(2,0.5,1,2,0),
                   c(1,2,1,3,0))
colnames(data) <- c('name', 'A1', 'A2', 'A3', 'A4')
data2 <- data.frame(c('A2','A4','A6','A7'),
                    c(1,0,1,2))
colnames(data2) <- c('ID', 'num')
data[, names(data) %in% append(data2$ID, "name")]
#>   name A2 A4
#> 1   AA  1  1
#> 2   BB  0  2
#> 3   CC  1  1
#> 4   DD  2  3
#> 5   EE  0  0

Created on 2022-07-20 by the reprex package (v2.0.1)

dplyr option using any_of:

library(dplyr)
data %>%
  select(name, any_of(data2$ID))
#>   name A2 A4
#> 1   AA  1  1
#> 2   BB  0  2
#> 3   CC  1  1
#> 4   DD  2  3
#> 5   EE  0  0

Created on 2022-07-20 by the reprex package (v2.0.1)

  • Related