Extract names of objects from list
Making a small tweak to the inside function and using lapply on an index instead of the actual list itself gets this doing what you want
x <- c("yes", "no", "maybe", "no", "no", "yes")
y <- c("red", "blue", "green", "green", "orange")
list.xy <- list(x=x, y=y)
WORD.C <- function(WORDS){
require(wordcloud)
L2 <- lapply(WORDS, function(x) as.data.frame(table(x), stringsAsFactors = FALSE))
# Takes a dataframe and the text you want to display
FUN <- function(X, text){
windows()
wordcloud(X[, 1], X[, 2], min.freq=1)
mtext(text, 3, padj=-4.5, col="red") #what I'm trying that isn't working
}
# Now creates the sequence 1,...,length(L2)
# Loops over that and then create an anonymous function
# to send in the information you want to use.
lapply(seq_along(L2), function(i){FUN(L2[[i]], names(L2)[i])})
# Since you asked about loops
# you could use i in seq_along(L2)
# instead of 1:length(L2) if you wanted to
#for(i in 1:length(L2)){
# FUN(L2[[i]], names(L2)[i])
#}
}
WORD.C(list.xy)
Extract names of dataframes in a list passed to a function in R
purrr::imap
comes in handy in this type of case where we need to iterate over the list of elements (or atomic vectors) and their names parallelly:
purrr::imap(list.ts, plot_func)
where plot_func <- function(df, name) plot(df, ylab="", xlab="Months", col="red", main = name)
R extract name of object within purrr::map()
We can use imap
(which calls map2
to pass the names index) instead of map
and extract the list
names with .y
and the values with .x
. Here imap
is calling map2
library(purrr)
imap(input, ~ .y)
#$df1
#[1] "df1"
#$df2
#[1] "df2"
Or instead of .x
, .y
, can also use ..1
and ..2
imap(input, ~ ..2)
Basically imap
is calling map2
imap
function (.x, .f, ...)
{
.f <- as_mapper(.f, ...)
map2(.x, vec_index(.x), .f, ...)
}
If we want to use map
, option is to loop over the names
or the sequence of list
map(names(input), ~ .x)
and for subsetting the values, use the [[
map(names(input), ~ input[[.x]])
With map
, the .x
is only the values of the data and the names of the list
are not passed into it
If we want to use base R
, then an option is Map
Map(function(u, v) v, input, names(input))
or using lapply
lapply(names(input), function(nm) nm)
R get objects' names from the list of objects
You can write your own list()
function so it behaves like data.frame()
, i.e., uses the un-evaluated arg names as entry names:
List <- function(...) {
names <- as.list(substitute(list(...)))[-1L]
setNames(list(...), names)
}
my.list <- List(model.product, model.i, model.add)
Then you can just access the names via:
names(my.list)
R: How to access the name of an element of a list?
Rephrasing the question, the goal is to create a list where the list elements have names according to the objects that have been assigned to them. If a
is assigned to the first list element, the first name should automatically become a
.
This can be achieved by using makeNamedList
instead of list
. The function makeNamedList
is a condensed result from this question.
makeNamedList <- function(...) {
structure(list(...), names = as.list(substitute(list(...)))[-1L])
}
Example:
mylist <- makeNamedList(cars, iris)
str(mylist)
# List of 2
# $ cars:'data.frame': 50 obs. of 2 variables:
# ..$ speed: num [1:50] 4 4 7 7 8 9 10 10 10 11 ...
# ..$ dist : num [1:50] 2 10 4 22 16 10 18 26 34 17 ...
# $ iris:'data.frame': 150 obs. of 5 variables:
# ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
# ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
# ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
# ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
# ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
So makeNamedList(cars, iris)
is equivalent to list(cars = cars, iris = iris)
.
Extract names of second [given] level of nested list in R
A possible solution, based on purrr::map_depth
:
library(tidyverse)
map_depth(x, 1, names) %>% unlist(use.names = F)
#> [1] "one_1" "one_2" "one_3" "two_1" "two_2"
How to get the name of each element of a list using lapply()?
Here's a solution using purrr
. It seems to run faster than the solution by aaronwolden but slower than akrun's solution (if that's important):
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"
Extract names of these objects and add to new array in React Native?
You can use the map function. If your original array is:
const array = [
{ id: 4, name: "Banana" },
{ id: 5, name: "Watermelon" },
{ id: 3, name: "Pineapple" },
]
you can create a new array by doing the following:
const namesArray = array.map((item) => item.name)
namesArray
is:
["Banana", "Watermelon", "Pineapple"]
Extract items in a list using variable names in R
Instead of dat_raw$`12`[[31]]
, you can have dat_raw[[12]][[31]]
if 12
is the 12th element of the JSON. So your for
loop would be:
for (m in 1:12) {
print(dat_raw[[m]][[31]])
}
Related Topics
Referring to Data.Table Columns by Names Saved in Variables
How to Call a Function Using the Character String of the Function Name in R
There Is Pmin and Pmax Each Taking Na.Rm, Why No Psum
How to Avoid: Read.Table Truncates Numeric Values Beginning with 0
Reproducing Lattice Dendrogram Graph with Ggplot2
Add (Subtract) Months Without Exceeding the Last Day of the New Month
Convert *Some* Column Classes in Data.Table
Most Frequent Value (Mode) by Group
How to Directly Select the Same Column from All Nested Lists Within a List
How to Insert an Image into the Navbar on a Shiny Navbarpage()
Speed Up Plot() Function for Large Dataset
Making a Stacked Area Plot Using Ggplot2
Using Row-Wise Column Indices in a Vector to Extract Values from Data Frame
Ggplot2 Heatmaps: Using Different Gradients for Categories