Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is is possible to convert a dataframe object to a tribble constructor?

Tags:

r

tidyverse

I have data that looks like this:

library(tidyverse)
df <- tibble(
    x = c(0, 179, 342, 467, 705, 878, 1080, 1209, 1458, 1639, 1805, 2000, 2121, 2339, 2462, 2676, 
      2857, 3049, 3227, 3403, 3583, 3651, 4009, 4034, 4151, 4194, 4512, 4523, 4679, 5789), 
    y = c(4.7005, 4.8598, 5.0876, 5.0938, 5.3891, 5.6095, 5.8777, 6.0064, 6.3063, 6.4723, 6.6053, 
          6.8145, 6.9078, 7.1701, 7.2633, 7.3865, 7.5766, 7.644, 7.8018, 7.9505, 8.0974, 8.1937, 
          8.2391, 8.294, 8.3143, 8.3452, 8.5092, 8.5172, 8.5993, 9.0275))

Is it possible to convert my dataframe/tibble object to a tribble "constructor"?

I'm looking for something like dput, but more lightweight and specifically for dataframes.

like image 981
emehex Avatar asked Mar 16 '17 16:03

emehex


People also ask

How do you use a Tribble in R?

tribble() creates a tibble and tricks you into typing out a preview of the result. To use tribble() , list each column name preceded by a ~ , then list the values of the tribble in a rowwise fashion. If you take care to align your columns, the transposed syntax of tribble() becomes a preview of the table.

What does Tibble function do in R?

Tibble is the central data structure for the set of packages known as the tidyverse, including dplyr, ggplot2, tidyr, and readr. Description This is a convenient way to add one or more columns to an existing data frame.

What is Tidyverse Tibble?

Tibbles are data. frames that are lazy and surly: they do less (i.e. they don't change variable names or types, and don't do partial matching) and complain more (e.g. when a variable does not exist). This forces you to confront problems earlier, typically leading to cleaner, more expressive code.


2 Answers

I think mc_tribble is a better name, and it looks like you can just condense it to:

mc_tribble <- function(indf, indents = 4, mdformat = TRUE) {
  name <- as.character(substitute(indf))
  name <- name[length(name)]

  meat <- capture.output(write.csv(indf, quote = TRUE, row.names = FALSE))
  meat <- paste0(
    paste(rep(" ", indents), collapse = ""),
    c(paste(sprintf("~%s", names(indf)), collapse = ", "),
      meat[-1]))

  if (mdformat) meat <- paste0("    ", meat)
  obj <- paste(name, " <- tribble(\n", paste(meat, collapse = ",\n"), ")", sep = "")
  if (mdformat) cat(paste0("    ", obj)) else cat(obj)
}

Try it out:

short_iris <- head(iris)

mc_tribble(short_iris)

Improvements:

  • Shorter code
  • Captures the name of the "tibble"
  • Has an argument for indenting
  • Has an argument for conveniently adding 4 spaces for pasting on Stack Overflow
  • Sounds more tasty

I've added this to my "SOfun" package. You can install it with:

source("http://news.mrdwab.com/install_github.R")
install_github("mrdwab/overflow-mrdwab") # for writeClip -- plus it's awesome
install_github("mrdwab/SOfun")

Usage is then simply:

library(SOfun)
mc_tribble(short_iris)

Advantages:

  • Now copies the output to your clipboard (if you have "overflow" installed)
  • Even more affordable than before!
like image 183
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 08 '22 00:10

A5C1D2H2I1M1N2O1R2T1


datapasta::dpasta() should be suitable. Output from your example:

dpasta(df)
tibble::tribble(
    ~x,     ~y,
     0, 4.7005,
   179, 4.8598,
   342, 5.0876,
   467, 5.0938,
   705, 5.3891,
   878, 5.6095,
  1080, 5.8777,
  1209, 6.0064,
  1458, 6.3063,
  1639, 6.4723,
  1805, 6.6053,
  2000, 6.8145,
  2121, 6.9078,
  2339, 7.1701,
  2462, 7.2633,
  2676, 7.3865,
  2857, 7.5766,
  3049,  7.644,
  3227, 7.8018,
  3403, 7.9505,
  3583, 8.0974,
  3651, 8.1937,
  4009, 8.2391,
  4034,  8.294,
  4151, 8.3143,
  4194, 8.3452,
  4512, 8.5092,
  4523, 8.5172,
  4679, 8.5993,
  5789, 9.0275
  )

https://cran.r-project.org/web/packages/datapasta/index.html https://github.com/MilesMcBain/datapasta

like image 23
MilesMcBain Avatar answered Oct 08 '22 00:10

MilesMcBain