Convert list to named list in R
So you want your list to have names:
NAME <- paste0("Degrees", 1:length(Degrees_df1))
Any of the following is OK:
names(Degrees_df1) <- NAME
attr(Degrees_df1, "names") <- NAME
Degrees_df1 <- "names<-"(Degrees_df1, NAME)
Degrees_df1 <- setNames(Degrees_df1, NAME)
Degrees_df1 <- structure(Degrees_df1, names = NAME)
But I think, the best thing is to give names when you create the list. For example, if you do:
list(1, 2, 3, 4)
the resulting list has no names. While if you do
list(a = 1, b= 2, c = 3, d = 4)
the resulting list has names.
If I am applying paste function over there I am getting error like this:
Error in assign(names(paste0("Degrees_df", i)), paste0("Degrees", 1:length(get(paste0("Degrees_df", : invalid first argument
Sorry, I wanted to modify your code inside for loop using paste function.
You possibly need this (not efficient):
df_i <- get(paste0("Degrees_df", i)) ## a local variable
names(df_i) <- paste0("Degrees", 1:length(df_i)) ## modify local variable
assign(paste0("Degrees_df", i), df_i) ## write back and update
or (better):
df_i <- get(paste0("Degrees_df", i)) ## a local variable
assign(paste0("Degrees_df", i),
setNames(df_i, paste0("Degrees", 1:length(df_i))))
# assign(paste0("Degrees_df", i),
# "names<-"(df_i, paste0("Degrees", 1:length(df_i))))
# assign(paste0("Degrees_df", i),
# structure(df_i, names = paste0("Degrees", 1:length(df_i))))
assign
is used to assign value (or another variable) to a variable. It looks like your error code tries to assign names attributes. Note, the names of a list / data.frame is "attributes", not a variable, so you can not use assign
to change them.
Converting grouped tibble to named list
We can use split
with(my_data, split(list_values,
factor(list_names, levels = unique(list_names))))
$Ford
[1] "Ranger" "F150" "Explorer"
$Chevy
[1] "Equinox"
$Dodge
[1] "Caravan" "Ram"
Or with unstack
unstack(my_data, list_values ~ list_names)
$Chevy
[1] "Equinox"
$Dodge
[1] "Caravan" "Ram"
$Ford
[1] "Ranger" "F150" "Explorer"
How to turn a named vector into a named list, while grouping entries with the same name into one list element?
We can do this with split
ting the vector
by the names
of the vector
tmp_n <- split(unname(tmp_v), names(tmp_v))
identical(tmp_n, tmp_l)
#[1] TRUE
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()
Convert named vector to list in R
Like I said in the comment, you can use split
to create a list.
a.list <- split(a, names(a))
a.list <- lapply(a.list, unname)
A one-liner would be
a.list <- lapply(split(a, names(a)), unname)
#$I
#[1] 1 2 3 4
#
#$II
#[1] 5 6 7 8
EDIT.
Then, thelatemail posted a simplification of this in his comment. I've timed it using Devin King's way and it's not only simpler it's also 25% faster.
a.list <- split(unname(a),names(a))
r data.frame to a named list
One option is to deframe
to create a named vector
and then use as.list
. to convert each element as a list
element
library(tibble)
library(dplyr)
deframe(d) %>%
as.list
Or in base R
with split
with(d, split(value, key))
R Unlist named list into one string with preserving list names
You can use paste()
to create the "=" separator and collapse with ";". names()
allows you to access the names in the list.
UPDATED with @Dason's suggestion.
paste(names(nl),nl,sep="=",collapse=";" )
[1] "case1=master2;case2=5;case3=master;case4=345"
Convert named list of lists to dataframe using tidy approach
We may use bind_rows
which have the .id
that creates a new column from the names
of the list
library(dplyr)
bind_rows(df, .id = "fitname")
# A tibble: 4 × 6
fitname term estimate std.error statistic p.value
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 e1m1_fit (Intercept) 2.7 0.03 88.0 0.01
2 e1m1_fit log10(q) -0.1 0.01 -15.6 0.01
3 e2m2a_fit (Intercept) 2.7 0.03 79.8 0.01
4 e2m2a_fit log10(q) -0.1 0.01 -15.5 0.01
In addition, if the df
list
was created by looping with map
, the _dfr
can return a single tibble/data.frame
with the .id
specified as the name of the list
library(purrr)
map_dfr(yourlist, ~ yourfun(.x), .id = "fitname")
convert named list with mixed content to data frame
We can use stack
from base R
stack(my_list)
According to ?stack
The stack function is used to transform data available as separate columns in a data frame or list into a single column that can be used in an analysis of variance model or other linear model. The unstack function reverses this operation.
Or with enframe
library(tidyverse)
enframe(my_list) %>% # creates the 'value' as a `list` column
mutate(value = map(value, as.character)) %>% # change to single type
unnest
Turn a dataframe into a named list of dataframes with names from a column in R
We can use:
library(tibble)
as.list(deframe(df))
Related Topics
Extract Hyperlink from Excel File in R
Print a List of Dynamically-Sized Plots in Knitr
Repeat Vector to Fill Down Column in Data Frame
Upload and View a PDF in R Shiny
Usage of Uioutput in Multiple Menuitems in R Shiny Dashboard
Importing Data into R (Rdata) from Github
Order X Axis Day Values in Ggplot2
R Group By, Counting Non-Na Values
Q-Q Plot with Ggplot2::Stat_Qq, Colours, Single Group
Knitr Compile Problems with Rstudio (Windows)