Giter Site home page Giter Site logo

Comments (13)

ijlyttle avatar ijlyttle commented on June 12, 2024 2

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.

jennybc avatar jennybc commented on June 12, 2024 1

I will try to propose a name.

from tibble.

krlmlr avatar krlmlr commented on June 12, 2024

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.

jennybc avatar jennybc commented on June 12, 2024

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.

jennybc avatar jennybc commented on June 12, 2024

In addition to id() and upname(), dub() is a possible name for this name-promoting variable-pre-processing function.

from tibble.

krlmlr avatar krlmlr commented on June 12, 2024

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.

jennybc avatar jennybc commented on June 12, 2024

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.

hadley avatar hadley commented on June 12, 2024

What if this was just the as_data_frame() method for vectors?

from tibble.

krlmlr avatar krlmlr commented on June 12, 2024

@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.

hadley avatar hadley commented on June 12, 2024

@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.

krlmlr avatar krlmlr commented on June 12, 2024

@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.

krlmlr avatar krlmlr commented on June 12, 2024

Note that this is already possible with #71:

x %>% as_data_frame %>% rownames_to_column

from tibble.

github-actions avatar github-actions commented on June 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.