Skip to content

Commit

Permalink
Extract ex04
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Apr 2, 2018
1 parent 139091a commit fa26cf5
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 144 deletions.
28 changes: 3 additions & 25 deletions inside-the-box.R → ex04_attack-via-rows-or-columns.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' ---
#' title: "Thinking Inside the Box"
#' title: "Attack via rows or columns?"
#' author: "Jenny Bryan"
#' date: "`r format(Sys.Date())`"
#' output: github_document
Expand All @@ -13,21 +13,18 @@ knitr::opts_chunk$set(
)
options(tidyverse.quiet = TRUE)

#' **WARNING: half-baked**

#+ body
# ----
library(tidyverse)


# ----
#' ## If you must sweat, compare row-wise work vs. column-wise work
#'
#' The approach you use in that first example is not always the one that scales
#' up the best.

sapply(df, class)
class(df[1, ])
class(iris[14, ])

x <- list(
list(name = "sue", number = 1, veg = c("onion", "carrot")),
list(name = "doug", number = 2, veg = c("potato", "beet"))
Expand All @@ -51,22 +48,3 @@ tibble(
number = map_dbl(x, "number"),
veg = map(x, "veg")
)

# add_row()
# rbind(df, as.data.frame(t(v2)))
# https://stackoverflow.com/questions/22581122/how-to-add-a-named-vector-as-a-row-to-a-data-frame

## iterating over rows
## specific problem from dean: transforming a dataframe into a list of rows
## (the format that Javascript d3 expects)
# split(x, seq_len(nrow(x))) then lapply (or purrr::map)
# lapply(seq_len(nrow(df)), function(i) as.list(df[i,,drop=F]))
# for (i in seq_len(nrow(df)) {...}
# for(i in seq_along(df[,1]){df[i,]}
# lapply(seq(nrow(df)), function(i) { row <- df[i,] }
# ## jim hester
# lapply(seq_len(NROW(df)), function(i) val = df[i, , drop = FALSE])

pmap(head(iris), list)

View(mtcars)
69 changes: 69 additions & 0 deletions ex04_attack-via-rows-or-columns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
Attack via rows or columns?
================
Jenny Bryan
2018-04-02

**WARNING: half-baked**

``` r
library(tidyverse)
```

## If you must sweat, compare row-wise work vs. column-wise work

The approach you use in that first example is not always the one that
scales up the best.

``` r
x <- list(
list(name = "sue", number = 1, veg = c("onion", "carrot")),
list(name = "doug", number = 2, veg = c("potato", "beet"))
)

# row binding

# frustrating base attempts
rbind(x)
#> [,1] [,2]
#> x List,3 List,3
do.call(rbind, x)
#> name number veg
#> [1,] "sue" 1 Character,2
#> [2,] "doug" 2 Character,2
do.call(rbind, x) %>% str()
#> List of 6
#> $ : chr "sue"
#> $ : chr "doug"
#> $ : num 1
#> $ : num 2
#> $ : chr [1:2] "onion" "carrot"
#> $ : chr [1:2] "potato" "beet"
#> - attr(*, "dim")= int [1:2] 2 3
#> - attr(*, "dimnames")=List of 2
#> ..$ : NULL
#> ..$ : chr [1:3] "name" "number" "veg"

# tidyverse fail
bind_rows(x)
#> Error in bind_rows_(x, .id): Argument 3 must be length 1, not 2
map_dfr(x, ~ .x)
#> Error in bind_rows_(x, .id): Argument 3 must be length 1, not 2

map_dfr(x, ~ .x[c("name", "number")])
#> # A tibble: 2 x 2
#> name number
#> <chr> <dbl>
#> 1 sue 1.
#> 2 doug 2.

tibble(
name = map_chr(x, "name"),
number = map_dbl(x, "number"),
veg = map(x, "veg")
)
#> # A tibble: 2 x 3
#> name number veg
#> <chr> <dbl> <list>
#> 1 sue 1. <chr [2]>
#> 2 doug 2. <chr [2]>
```
119 changes: 0 additions & 119 deletions inside-the-box.md

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit fa26cf5

Please sign in to comment.