convert named list with mixed content to data frame
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
Is there a better and nicer way to convert named list with mixed content to data frame?
The working example:
my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)
my_df <- data.frame(
"key" = names(my_list),
stringsAsFactors = F
)
my_df[["value"]] <- unname(my_list)
Is it possible to do this conversion in one step?
r dataframe
add a comment |
up vote
6
down vote
favorite
Is there a better and nicer way to convert named list with mixed content to data frame?
The working example:
my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)
my_df <- data.frame(
"key" = names(my_list),
stringsAsFactors = F
)
my_df[["value"]] <- unname(my_list)
Is it possible to do this conversion in one step?
r dataframe
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
Is there a better and nicer way to convert named list with mixed content to data frame?
The working example:
my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)
my_df <- data.frame(
"key" = names(my_list),
stringsAsFactors = F
)
my_df[["value"]] <- unname(my_list)
Is it possible to do this conversion in one step?
r dataframe
Is there a better and nicer way to convert named list with mixed content to data frame?
The working example:
my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)
my_df <- data.frame(
"key" = names(my_list),
stringsAsFactors = F
)
my_df[["value"]] <- unname(my_list)
Is it possible to do this conversion in one step?
r dataframe
r dataframe
edited 8 hours ago
Ronak Shah
27.3k93551
27.3k93551
asked 9 hours ago
RSzT
655
655
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
9
down vote
accepted
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
Hi @akun,stack
is simple and quick but I don't find the help document. Would you like to add the package name (base?) or explain it a little bit?
– Jiaxiang
9 hours ago
@Jiaxiang it is frombase R
. 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.
. Here, it is a named list. So, it unlist the values and at the same time, replicate the names of the list as a separate column
– akrun
9 hours ago
@akrun if I understand correctly,stack
solution needs another step withcolnames
to change names of columns to key and value? There is no other trick to do this in one operation?
– RSzT
9 hours ago
@RSzT You can usesetNames(stack(my_list)[2:1], c("key", "value"))
as the default columns are named with 'ind' and 'values' where as inenframe
there is an option to change the name in the argument
– akrun
9 hours ago
@akrun It seems thatind
column (orkey
after above) is a factor.
– RSzT
6 hours ago
|
show 1 more comment
up vote
1
down vote
You can use dplyr::as_tibble
to coerce the list into a data frame / tibble. This will automatically create a data frame where the list's names are column names, and the list items correspond to rows.
library(dplyr)
library(tidyr)
my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)
as_tibble(my_list)
#> # A tibble: 1 x 3
#> a b c
#> <dbl> <chr> <lgl>
#> 1 1 foo TRUE
To reshape into the two-column format you have, pipe it into tidyr::gather
, where the default column names are key and value. Because of the different data types in the column value
, this will coerce all the values to character.
as_tibble(my_list) %>%
gather()
#> # A tibble: 3 x 2
#> key value
#> <chr> <chr>
#> 1 a 1
#> 2 b foo
#> 3 c TRUE
Created on 2018-11-09 by the reprex package (v0.2.1)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
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
Hi @akun,stack
is simple and quick but I don't find the help document. Would you like to add the package name (base?) or explain it a little bit?
– Jiaxiang
9 hours ago
@Jiaxiang it is frombase R
. 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.
. Here, it is a named list. So, it unlist the values and at the same time, replicate the names of the list as a separate column
– akrun
9 hours ago
@akrun if I understand correctly,stack
solution needs another step withcolnames
to change names of columns to key and value? There is no other trick to do this in one operation?
– RSzT
9 hours ago
@RSzT You can usesetNames(stack(my_list)[2:1], c("key", "value"))
as the default columns are named with 'ind' and 'values' where as inenframe
there is an option to change the name in the argument
– akrun
9 hours ago
@akrun It seems thatind
column (orkey
after above) is a factor.
– RSzT
6 hours ago
|
show 1 more comment
up vote
9
down vote
accepted
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
Hi @akun,stack
is simple and quick but I don't find the help document. Would you like to add the package name (base?) or explain it a little bit?
– Jiaxiang
9 hours ago
@Jiaxiang it is frombase R
. 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.
. Here, it is a named list. So, it unlist the values and at the same time, replicate the names of the list as a separate column
– akrun
9 hours ago
@akrun if I understand correctly,stack
solution needs another step withcolnames
to change names of columns to key and value? There is no other trick to do this in one operation?
– RSzT
9 hours ago
@RSzT You can usesetNames(stack(my_list)[2:1], c("key", "value"))
as the default columns are named with 'ind' and 'values' where as inenframe
there is an option to change the name in the argument
– akrun
9 hours ago
@akrun It seems thatind
column (orkey
after above) is a factor.
– RSzT
6 hours ago
|
show 1 more comment
up vote
9
down vote
accepted
up vote
9
down vote
accepted
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
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
edited 9 hours ago
answered 9 hours ago
akrun
387k13172249
387k13172249
Hi @akun,stack
is simple and quick but I don't find the help document. Would you like to add the package name (base?) or explain it a little bit?
– Jiaxiang
9 hours ago
@Jiaxiang it is frombase R
. 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.
. Here, it is a named list. So, it unlist the values and at the same time, replicate the names of the list as a separate column
– akrun
9 hours ago
@akrun if I understand correctly,stack
solution needs another step withcolnames
to change names of columns to key and value? There is no other trick to do this in one operation?
– RSzT
9 hours ago
@RSzT You can usesetNames(stack(my_list)[2:1], c("key", "value"))
as the default columns are named with 'ind' and 'values' where as inenframe
there is an option to change the name in the argument
– akrun
9 hours ago
@akrun It seems thatind
column (orkey
after above) is a factor.
– RSzT
6 hours ago
|
show 1 more comment
Hi @akun,stack
is simple and quick but I don't find the help document. Would you like to add the package name (base?) or explain it a little bit?
– Jiaxiang
9 hours ago
@Jiaxiang it is frombase R
. 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.
. Here, it is a named list. So, it unlist the values and at the same time, replicate the names of the list as a separate column
– akrun
9 hours ago
@akrun if I understand correctly,stack
solution needs another step withcolnames
to change names of columns to key and value? There is no other trick to do this in one operation?
– RSzT
9 hours ago
@RSzT You can usesetNames(stack(my_list)[2:1], c("key", "value"))
as the default columns are named with 'ind' and 'values' where as inenframe
there is an option to change the name in the argument
– akrun
9 hours ago
@akrun It seems thatind
column (orkey
after above) is a factor.
– RSzT
6 hours ago
Hi @akun,
stack
is simple and quick but I don't find the help document. Would you like to add the package name (base?) or explain it a little bit?– Jiaxiang
9 hours ago
Hi @akun,
stack
is simple and quick but I don't find the help document. Would you like to add the package name (base?) or explain it a little bit?– Jiaxiang
9 hours ago
@Jiaxiang it is from
base R
. 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.
. Here, it is a named list. So, it unlist the values and at the same time, replicate the names of the list as a separate column– akrun
9 hours ago
@Jiaxiang it is from
base R
. 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.
. Here, it is a named list. So, it unlist the values and at the same time, replicate the names of the list as a separate column– akrun
9 hours ago
@akrun if I understand correctly,
stack
solution needs another step with colnames
to change names of columns to key and value? There is no other trick to do this in one operation?– RSzT
9 hours ago
@akrun if I understand correctly,
stack
solution needs another step with colnames
to change names of columns to key and value? There is no other trick to do this in one operation?– RSzT
9 hours ago
@RSzT You can use
setNames(stack(my_list)[2:1], c("key", "value"))
as the default columns are named with 'ind' and 'values' where as inenframe
there is an option to change the name in the argument– akrun
9 hours ago
@RSzT You can use
setNames(stack(my_list)[2:1], c("key", "value"))
as the default columns are named with 'ind' and 'values' where as inenframe
there is an option to change the name in the argument– akrun
9 hours ago
@akrun It seems that
ind
column (or key
after above) is a factor.– RSzT
6 hours ago
@akrun It seems that
ind
column (or key
after above) is a factor.– RSzT
6 hours ago
|
show 1 more comment
up vote
1
down vote
You can use dplyr::as_tibble
to coerce the list into a data frame / tibble. This will automatically create a data frame where the list's names are column names, and the list items correspond to rows.
library(dplyr)
library(tidyr)
my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)
as_tibble(my_list)
#> # A tibble: 1 x 3
#> a b c
#> <dbl> <chr> <lgl>
#> 1 1 foo TRUE
To reshape into the two-column format you have, pipe it into tidyr::gather
, where the default column names are key and value. Because of the different data types in the column value
, this will coerce all the values to character.
as_tibble(my_list) %>%
gather()
#> # A tibble: 3 x 2
#> key value
#> <chr> <chr>
#> 1 a 1
#> 2 b foo
#> 3 c TRUE
Created on 2018-11-09 by the reprex package (v0.2.1)
add a comment |
up vote
1
down vote
You can use dplyr::as_tibble
to coerce the list into a data frame / tibble. This will automatically create a data frame where the list's names are column names, and the list items correspond to rows.
library(dplyr)
library(tidyr)
my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)
as_tibble(my_list)
#> # A tibble: 1 x 3
#> a b c
#> <dbl> <chr> <lgl>
#> 1 1 foo TRUE
To reshape into the two-column format you have, pipe it into tidyr::gather
, where the default column names are key and value. Because of the different data types in the column value
, this will coerce all the values to character.
as_tibble(my_list) %>%
gather()
#> # A tibble: 3 x 2
#> key value
#> <chr> <chr>
#> 1 a 1
#> 2 b foo
#> 3 c TRUE
Created on 2018-11-09 by the reprex package (v0.2.1)
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use dplyr::as_tibble
to coerce the list into a data frame / tibble. This will automatically create a data frame where the list's names are column names, and the list items correspond to rows.
library(dplyr)
library(tidyr)
my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)
as_tibble(my_list)
#> # A tibble: 1 x 3
#> a b c
#> <dbl> <chr> <lgl>
#> 1 1 foo TRUE
To reshape into the two-column format you have, pipe it into tidyr::gather
, where the default column names are key and value. Because of the different data types in the column value
, this will coerce all the values to character.
as_tibble(my_list) %>%
gather()
#> # A tibble: 3 x 2
#> key value
#> <chr> <chr>
#> 1 a 1
#> 2 b foo
#> 3 c TRUE
Created on 2018-11-09 by the reprex package (v0.2.1)
You can use dplyr::as_tibble
to coerce the list into a data frame / tibble. This will automatically create a data frame where the list's names are column names, and the list items correspond to rows.
library(dplyr)
library(tidyr)
my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)
as_tibble(my_list)
#> # A tibble: 1 x 3
#> a b c
#> <dbl> <chr> <lgl>
#> 1 1 foo TRUE
To reshape into the two-column format you have, pipe it into tidyr::gather
, where the default column names are key and value. Because of the different data types in the column value
, this will coerce all the values to character.
as_tibble(my_list) %>%
gather()
#> # A tibble: 3 x 2
#> key value
#> <chr> <chr>
#> 1 a 1
#> 2 b foo
#> 3 c TRUE
Created on 2018-11-09 by the reprex package (v0.2.1)
answered 8 hours ago
camille
6,20431227
6,20431227
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230519%2fconvert-named-list-with-mixed-content-to-data-frame%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password