How to get the name of each list item using lapply ()?

Imagine I have the following list

> test <- list("a" = 1, "b" = 2) 

Each list item has a name:

 > names(test) 

Now I want to extract this name using lapply() , because I want to use it in a new function that will be called using lapply. I just don't know how to extract the name of each element.

I tried using deparse() and substitute() , but the result was strange:

 > lapply(test, function(x) {deparse(substitute(x))}) $a [1] "X[[i]]" $b [1] "X[[i]]" 

Somebody knows?

Accuracy:

I want to do something like this: I have a list that looks like a test:

 > test <- list("a" = matrix(1, ncol = 3), "b" = matrix(2, ncol = 3)) 

I want to apply a function to this list that converts data inside each element and gives a specific name for each column:

 make_df <- function(x) { output <- data.frame(x) names(output) <- c("items", "type", NAME_OF_X) return(output) } lapply(test, make_df) 

Expected Result:

 > test $a [,1] [,2] [,3] [1,] 1 1 1 attr(,"names") [1] "index" "type" "a" $b [,1] [,2] [,3] [1,] 2 2 2 attr(,"names") [1] "index" "type" "b" 

I do not know how I can get the name of an element to indicate a name for my third column.

+5
source share
3 answers

Here's a solution using purrr . It seems to be faster than aaronwolden solution, but slower than akrun solution (if that matters):

 library(purrr) map2(test, names(test), function(vec, name) { names(vec) <- c("index", "type", name) return(vec) }) $a [,1] [,2] [,3] [1,] 1 1 1 attr(,"names") [1] "index" "type" "a" $b [,1] [,2] [,3] [1,] 2 2 2 attr(,"names") [1] "index" "type" "b" 
+3
source

Assuming that you want both test elements to contain a 3-column matrix, you can use mapply() and list the list and list names separately:

  test <- list("a" = matrix(1, ncol = 3), "b" = matrix(2, ncol = 3)) make_df <- function(x, y) { output <- data.frame(x) names(output) <- c("items", "type", y) return(output) } mapply(make_df, x = test, y = names(test), SIMPLIFY = FALSE) 

which produces:

 ## $a ## items type a ## 1 1 1 1 ## ## $b ## items type b ## 1 2 2 2 

Update

To achieve the expected result that you describe in your updated question:

 test.names <- lapply(names(test), function(x) c("index", "type", x)) Map(setNames, test, test.names) 

gives:

 ## $a ## [,1] [,2] [,3] ## [1,] 1 1 1 ## attr(,"names") ## [1] "a" "index" "type" ## ## $b ## [,1] [,2] [,3] ## [1,] 2 2 2 ## attr(,"names") ## [1] "b" "index" "type" 
+4
source

Based on the expected result, it was shown

  make_attr_names <- function(x){ x1 <- test[[x]] attr(x1, 'names') <- c('items','type', x) x1} lapply(names(test), make_attr_names) #[[1]] # [,1] [,2] [,3] #[1,] 1 1 1 #attr(,"names") #[1] "items" "type" "a" #[[2]] # [,1] [,2] [,3] #[1,] 2 2 2 #attr(,"names") #[1] "items" "type" "b" 

Or based on the description

  make_df <- function(x){ setNames(as.data.frame(test[[x]]), c('items', 'type', x))} lapply(names(test), make_df) #[[1]] # items type a #1 1 1 1 #[[2]] # items type b #1 2 2 2 
+2
source

All Articles