Home > database >  getting summary of names in two data frames
getting summary of names in two data frames

Time:10-04

I have two data frames of more than 200 columns and i want to create a summary to check if the names of columns are matching. i want o create a function for that.

i want to compare matching names of two data frame if they matching exactly then join them other with bind that in new row.

df <- data.frame(EXT=c(1),MAN=c(2),MANi=c(2),nune=c(2),klay=c(4),emial=c("dd"),Pass=c(99),fri=c("TGA"),
                 mkl=c("nhi"),kin=c(7),munc=c(6),lone=c(44),wond=c("tko"))

df1 <- data.frame(EXT=c(1),MAN=c(2),MANi=c(2),nune=c(2),klay=c(4),emial=c("dd"),PASS=c(99),fri=c("TGA"),
                 mkl=c("nhi"),kin=c(7),MUNC=c(6),lone=c(44),hulu=c("kra"),kone=("hab"))

the required output should be like

enter image description here

CodePudding user response:

compare_names <- function(df1, df2) {
  # Get All Names to start with.
  out <- data.frame(names1 = unique(c(names(df1), names(df2))))

  # Copy them over.
  out$names2 <- out$names1

  # Set them to NA if they are not present.
  out$names1[!(out$names1 %in% names(df1))] <- NA
  out$names2[!(out$names2 %in% names(df2))] <- NA

  # If they are not NA in both columns above then they are matching.
  out$matching <- !is.na(out$names1) & !is.na(out$names2)
  out
}
  •  Tags:  
  • r
  • Related