user41912
user41912

Reputation: 567

Accessing lapply column names

If I am doing

lapply(dataframe, function(x) {
    column.name <- #insert code here
})

How would I be able to access the name of the column that the lapply function is currently processing? I want to assign the name of the column to a variable, column.name, as indicated in the code. Just to clarify, yes, column.name WILL change with each iteration of the lapply.

Upvotes: 12

Views: 11287

Answers (3)

alistaire
alistaire

Reputation: 43334

You can iterate over an index, but this is not very R-like code. A more direct route is to use Map, the multivariate version of lapply, which iterates a function of appropriate arity in parallel across whatever parameters are passed to it:

Map(function(value, name){paste(name, sum(value), sep = ": ")}, 
    Formaldehyde, 
    names(Formaldehyde))
#> $carb
#> [1] "carb: 3.1"
#> 
#> $optden
#> [1] "optden: 2.747"

If using the tidyverse, purrr::imap is a similar convenience version of purrr::map2 that automatically uses the names of the first parameter as a second parameter:

purrr::imap(Formaldehyde, ~paste(.y, sum(.x), sep = ": "))
#> $carb
#> [1] "carb: 3.1"
#> 
#> $optden
#> [1] "optden: 2.747"

Versions of each that simplify are available: for Map, mapply, a multivariate sapply (of which Map is technically just a wrapper with SIMPLIFY = FALSE); for imap, versions with a subscript of the type to simplify to, e.g. imap_chr.

Upvotes: 5

Mox
Mox

Reputation: 531

How to pass a variable into the function while using lapply

a lapply with two variables so I don't have to keep rewriting the function for each state.

library(tidycensus)    
get_Census <- function(x,y) {
      get_decennial(geography = "block group",
                    variables = "P001001",
                    sumfile = "sf1",
                    key = mykey,
                    state = x, county = y,year = "2000",
                    geometry = FALSE)
    }
    CO<-c("067","073","113")
    lapply(CO,get_Census,x="06")

Upvotes: 1

tonytonov
tonytonov

Reputation: 25608

There is a way, actually.

df <- data.frame(a = 1:2, b = 3:4, c = 5:6)
lapply(df, function(x) names(df)[substitute(x)[[3]]])
$a
[1] "a"

$b
[1] "b"

$c
[1] "c"

But that should be used as a last resort. Instead, use something like (another option is given in comments)

lapply(seq_along(df), function(x) names(df[x]))
[[1]]
[1] "a"

[[2]]
[1] "b"

[[3]]
[1] "c"

Upvotes: 11

Related Questions