Home > Net >  How to write a lapply function in this case?
How to write a lapply function in this case?

Time:01-06

I'd like to compare the same column between two data frames using the lapply function in R, but have no idea how to do it.

A simplified example is like this:

I have a list of columns

col_ls <- c(c1,c2,c3,c4,c5)

and any function (e.g. intersect) to compare those columns (cx means column x below) between df1 and df2

any_function(df1$cx, df2$cx)

How can I write my own function to solve this with lapply?

CodePudding user response:

You can select columns via strings using [[.

The comparison function can then be mapped over the columns with the dataframes as arguments.

any_function <- function(fieldname, df1, df2) {
  df1[[fieldname]] == df2[[fieldname]]
}
lapply(col_ls, any_function, onedf, otherdf)

CodePudding user response:

A mapply example, using mtcars

set.seed(1)

col_ls <- c("mpg","cyl","disp")

mapply(
  function(x,y){
    intersect(x,y)
  },
  mtcars[col_ls],
  mtcars[col_ls] sample(c(-1,0,1),prod(dim(mtcars[col_ls])),replace=T)
)

resulting in

$mpg
 [1] 22.8 21.4 24.4 10.4 14.7 32.4 27.3 15.8 19.7 15.0

$cyl
[1] 6 4 8

$disp
[1] 108.0 167.6 472.0 120.1 120.3  95.1 351.0 301.0 121.0
  •  Tags:  
  • Related