Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing lapply column names

Tags:

r

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.

like image 994
user41912 Avatar asked Jul 16 '14 17:07

user41912


People also ask

Does Lapply return a list?

lapply returns a list of the same length as X , each element of which is the result of applying FUN to the corresponding element of X .

How do I rename a column in a list in R?

To rename a column in R you can use the rename() function from dplyr. For example, if you want to rename the column “A” to “B”, again, you can run the following code: rename(dataframe, B = A) .

Can you use Lapply on a Dataframe in R?

In R Programming Language to apply a function to every integer type value in a data frame, we can use lapply function from dplyr package. And if the datatype of values is string then we can use paste() with lapply.


3 Answers

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"
like image 117
tonytonov Avatar answered Sep 27 '22 18:09

tonytonov


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.

like image 41
alistaire Avatar answered Sep 27 '22 17:09

alistaire


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")
like image 20
Mox Avatar answered Sep 27 '22 18:09

Mox