Search code examples
classlapplypurrr

Can I use lapply or a similar function to add a class to selected columns of a data frame?


This data frame

> jnk <- mtcars[1:3, 1:3]
> jnk

               mpg cyl disp
Mazda RX4     21.0   6  160
Mazda RX4 Wag 21.0   6  160
Datsun 710    22.8   4  108

has classes as follows:

> lapply(jnk, class)
$mpg
[1] "numeric"

$cyl
[1] "numeric"

$disp
[1] "numeric"

Let's say I defined a class "formula" and want to add that class to columns 2 & 3:

> myfunc <- function(df, cols){
    for(i in cols){
      class(df[[i]]) <- as.list( c(class(df[[i]]), as.list("formula")) )
    }
    return(df)
  }

> jnk <- myfunc(jnk, 2:3)
> lapply(jnk, class)

$mpg
[1] "numeric"

$cyl
[1] "numeric" "formula"

$disp
[1] "numeric" "formula"

How can I do this elegantly, perhaps using lapply or a similar function from purrr? Ideally, by deploying a function that will work on a DF and column indexes? Or have I already arrived at the "elegant" solution?

I tried the following (first reseting jnk <- mtcars[1:3, 1:3]), but I'm clearly confused about how this works:

> newfunc <- function(df, colnums){
 lapply(colnums, function(i) {
    class(df[[i]]) <- as.list( c(class(df[[i]]), as.list("formula")) )
    }
 )
 return(df)
}

> jnk <- newfunc(jnk, 2:3)

> lapply(jnk, class)

$mpg
[1] "numeric"

$cyl
[1] "numeric"

$disp
[1] "numeric"

Solution

  • Hmmm are you wanting something like this?

    f <- function(x) {
        class(x) <- c(class(x), "formula")
        x
    }
    
    df <- map(jnk, f)
    map(df, class)