Naming List Elements in R

Naming list elements in R

The first case is the correct usage. In the second case you are sending filList[i] to names<- which is only exists as a temporary subsetted object.

Alternatively, you could just do everything outside the loop with:

names(filList) <- names(Fil)

How to name a list object in R

If you want to name the lists while creating them you can use tibble::lst

my_df1 <- mtcars[1:10,]
my_df2 <- mtcars[11:20,]
my_df3 <- mtcars[21:30,]
my_list <- tibble::lst(my_df1, my_df2, my_df3)

If the list is already created you can use names or setNames to name the list.

names(my_list) <- c('list1', 'list2', 'list3')

setNames(my_list, c('list1', 'list2', 'list3'))

how to name the element list automatically in R (based on the number of the elements)

library(purrr)
the_list <- map(1:5, ~.x)

names <- paste0('result', as.character(seq(1, length(the_list))))
purrr::set_names(the_list, names)
#> $result1
#> [1] 1
#>
#> $result2
#> [1] 2
#>
#> $result3
#> [1] 3
#>
#> $result4
#> [1] 4
#>
#> $result5
#> [1] 5

more_names <-
map(1:5, ~ rep(1, .x)) %>%
map(~length(.x)) %>%
map(~paste0('result', as.character(seq(1, .x))) )

map(1:5, ~ vector('list', .x)) %>%
map2(more_names, ~ set_names(.x, .y))
#> [[1]]
#> [[1]]$result1
#> NULL
#>
#>
#> [[2]]
#> [[2]]$result1
#> NULL
#>
#> [[2]]$result2
#> NULL
#>
#>
#> [[3]]
#> [[3]]$result1
#> NULL
#>
#> [[3]]$result2
#> NULL
#>
#> [[3]]$result3
#> NULL
#>
#>
#> [[4]]
#> [[4]]$result1
#> NULL
#>
#> [[4]]$result2
#> NULL
#>
#> [[4]]$result3
#> NULL
#>
#> [[4]]$result4
#> NULL
#>
#>
#> [[5]]
#> [[5]]$result1
#> NULL
#>
#> [[5]]$result2
#> NULL
#>
#> [[5]]$result3
#> NULL
#>
#> [[5]]$result4
#> NULL
#>
#> [[5]]$result5
#> NULL

Created on 2021-06-06 by the reprex package (v2.0.0)

Give name to list variable

Since @akrun doesn't need any more points, here is an example showing how you can assign names to a list:

lst <- list(a="one", b="two", c=c(1:3))
names(lst)
[1] "a" "b" "c"
names(lst) <- c("x", "y", "z")

> lst
$x
[1] "one"

$y
[1] "two"

$z
[1] 1 2 3

Assign names to index elements in large list - R

mycars <- setNames(mycars,c("fast","medium","slow"))

you can still use indexes :

mycars[[1]]
mycars[["fast"]]

names(mycars[[1]]) returns the column names of mycars[[1]].

One thing that might be counterintuitive is that a list in R has an optional names attribute of the same length as the number of columns, but the column items don't have their name attached to them (there's no singular name attribute).

By the time you call mycars[[1]] you lost the names. If you call mycars[1] however you get a list of one element only, and this list can have a names attribute of length one.

How to name the elements of different R list elements?

setNames is the equivalent to `names<-` for functional programming:

lapply(test[-1], setNames, test$a)
#$b
#name1 name2 name3
# 0.1 0.2 0.3
#
#$c
#name1 name2 name3
# TRUE FALSE TRUE

Name list elements based on variable names R

You cannot assign to paste() using the <- operator (and I believe this is true for the eval() function as well). Instead, you need to either use the [[ operator or use the names() function. You can do this like so:

L <- list()
var1 <- "wood"
var2 <- 1.0
var3 <- "z4"
varname <- paste(var1, as.character(var2), var3, sep="_")

# Using [[
L[[varname]] <- c(0,1)

> L
$wood_1_z4
[1] 0 1

# Using names()
L[[1]] <- c(0,1)
names(L)[1] <- varname

> L
$wood_1_z4
[1] 0 1

A more effective way to do this might be to use a function that both creates the value and names the list element, or even one that just creates the value - if you then use sapply you can name each list element using arguments from the call to your function and the USE.NAMES options.

In a general sense, R isn't really well-optimized for growing lists over time when the final size and structure of the list aren't well-known. While it can be done, a more "R-ish" way to do it would be to figure out the structure ahead of time.

Converting the names of a list elements as a variable in a data.frame

When I think of iterating over a vector I think of the map functions in purrr. In this case, map2() iterates over multiple arguments simultaneously. I grabbed the names of the list elements and stored them in a vector, n:

n <- names(List)

I then iterated over List and n at the same time, calling cbind() to add a variable (i.e., column) to each data frame:

map2(List, n, ~ cbind(.x, Name = .y))

Output:


$bar1
study sd Name
1 A 1 bar1

$bar2
study sd Name
1 B 2 bar2
2 C 3 bar2

$bar3
study sd Name
1 Z 4 bar3

$bar4
study sd Name
1 H 5 bar4

We can collapse List to a single data frame with a call to bind_rows():

result <- map2(List, n, ~ cbind(.x, Name = .y))
bind_rows(result)

Output:


study sd Name
1 A 1 bar1
2 B 2 bar2
3 C 3 bar2
4 Z 4 bar3
5 H 5 bar4

Taken as one statement that would be:

map2(List, names(List), ~ cbind(.x, Name = .y)) %>% 
bind_rows()


Related Topics



Leave a reply



Submit