Comments (13)
Apologies if this is not helpful, but could purrr::map_df
be useful?
library("dplyr")
library("purrr")
x <- list(alpha = 'horrible', beta = 'list', gamma = 'column')
x %>% map_df(~ data_frame(thing = .x), .id = "name")
Could a new verb be put in place of the function within map_df
?
from tibble.
I will try to propose a name.
from tibble.
What verb would you use for this operation?
How about:
x %>% as_data_frame %>% tidyr::gather(id, thing)
Need to coerce to list if x
is a vector. The following doesn't work:
x %>% tidyr::gather(id, thing)
I think this could be fixed by implementing gather_.list <- function(x, ...) gather_(tibble::data_frame(x), ...)
.
EDIT: The above also doesn't work if x
is unnamed, but here you could use x %>% data_frame(thing=.) %>% add_rownames
.
EDIT²: I think a new verb would help here, I'm not sure if this belongs here or in tidyr.
from tibble.
The more I think about it, maybe it makes sense to think about this as a treatment applied to a variable during the construction of a data frame. A way to say "add this variable AND promote its names to a proper variable". And also give some nice way of getting row numbers into the data frame? I have found dplyr::row_number()
to be quite confusing / disappointing.
I realize id()
is probably already overloaded with meaning already. upname()
isn't great either, but hopefully this conveys the idea.
library(tibble)
x <- list(alpha = 'horrible', beta = 'list', gamma = 'column')
What if something like this:
df <- data_frame(id(x, "greek"))
## or
df <- data_frame(upname(x, "greek"))
produced this result
data_frame(greek = names(x), x = x)
#> Source: local data frame [3 x 2]
#>
#> greek x
#> (chr) (list)
#> 1 alpha <chr[1]>
#> 2 beta <chr[1]>
#> 3 gamma <chr[1]>
I also wish it were easier to get plain row numbers. I wish this is what row_number()
did but clearly it does not. So, again, fiction! What if something like this:
df <- data_frame(i = row_number(), upname(x, "greek"))
produced something like this:
data_frame(i = seq_along(x),
greek = names(x),
x = x)
#> Source: local data frame [3 x 3]
#>
#> i greek x
#> (int) (chr) (list)
#> 1 1 alpha <chr[1]>
#> 2 2 beta <chr[1]>
#> 3 3 gamma <chr[1]>
from tibble.
In addition to id()
and upname()
, dub()
is a possible name for this name-promoting variable-pre-processing function.
from tibble.
Row numbers: Have you seen #11? Your example would be then
data_frame(...) %>% rownames_to_column("i")
How would you like:
dub <- function(x) as_data_frame(setNames(list(names(x), x), c("name", "value")))
This allows at least the creation of a two-column data frame from a named object, which then can be massaged further with the other dplyr verbs, and combined with other data frames using cbind().
@hadley: Would this perhaps be suitable for purrr:
unzip_names <- function(x) set_names(list(x, names(x)), c("name", "value"))
zip_names <- function(x) set_names(x[[1]], x[[2]])
from tibble.
Sorry I can't really tell what #11 does just from reading the discussion. But I take your word for it that it would add the integers 1 through nrows(.)
as variable i
. Would it add as the first or last variable? Feels like you usually want it at the very front.
library(tibble)
x <- list(alpha = 'horrible', beta = 'list', gamma = 'column')
dub <- function(x) as_data_frame(setNames(list(names(x), x), c("name", "value")))
dub(x)
#> Source: local data frame [3 x 2]
#>
#> name value
#> (chr) (list)
#> 1 alpha <chr[1]>
#> 2 beta <chr[1]>
#> 3 gamma <chr[1]>
The variables themselves and the object look great. Would dub()
gain some arguments or different defaults in order to produce less generic names?
UPDATE: I think x
and names of x
are reversed in those speculative purrr
functions.
from tibble.
What if this was just the as_data_frame()
method for vectors?
from tibble.
@jennybc: purrr: Right, revised definition below.
unzip_names <- function(x) set_names(list(names(x), x), c("name", "value"))
rownames_to_column() will add to the front. Actually, we already have add_rownames() in dplyr, but it does "too much" and will be deprecated in favor of the new functions.
Defaults: I think we should support them, even if the renaming could be handle with a simple rename()
step. tidyr::gather()
has them too.
@hadley: Is there a dispatch for vector
, as in as_data_frame.vector()
? Otherwise it looks like we need to have as_data_frame.logical()
, as_data_frame.character()
, ...; we also need as_data_frame.Date()
, as_data_frame.POSIXt()
, ..., all with the same implementation. It also includes a certain amount of surprise -- if functions use as_data_frame() to convert input, the column names are auto-generated and the user might be unaware of it. Do you think it's worth it?
from tibble.
@krlmlr No, there's no "vector" virtual class, so implementation would be a bit tedious. But we don't need to have methods for Date
and factor
etc, because those will be caught by the method for the underlying atomic vector.
It seems like we're adding new functionality that previously was an error, so it doesn't seem too dangerous to me.
from tibble.
@jennybc: For now you could try kimisc::list_to_df() -- I totally forgot about this guy. I still think this should be part of tibble.
from tibble.
Note that this is already possible with #71:
x %>% as_data_frame %>% rownames_to_column
from tibble.
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.
from tibble.
Related Issues (20)
- tibble() behaves oddly with classed list columns
- `match(<num>, <num>)` fails on R 4.3.0.
- add_case/add_row silently converts factors to strings
- FR: Show column types like readr in the footer
- Feature request: Suppress message of new names being created in `tibble()` HOT 4
- YAML headers in Tibble
- Tweak the view function
- Calling tibble::rowid_to_column on an object of class "sf" before dplyr::rename will cause " internal error: can't find agr columns" HOT 5
- difference result between data.frame() with tibble()
- `as_tibble.data.table()` method? HOT 3
- Could `as_tibble.data.frame()` be stricter? HOT 2
- Error in stopifnot(is.character(filename), length(filename) == 1L) : reached elapsed time limit HOT 3
- Tibble creation not inputting (or displaying) correct values HOT 2
- create tibble with numeric(0) doesn't give same result as NULL column HOT 1
- Warn with `drop = TRUE` ?
- Weird row names inconsistency HOT 1
- Negative values after "num()" turned into a strange code, like "\033[31m0\033[39m\033[31m.\033......" HOT 1
- as_tibble.data.frame() treats attr()-type attributes inconsistently: sripts "n" but not others. HOT 2
- `tibble` 3.2.1.9017: some errors in running vignette code HOT 15
- tibble 3.2.1 testing fails on Windows R-4.3.2 HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tibble.