Skip to content

Instantly share code, notes, and snippets.

@jmbarbone
Created November 10, 2022 18:13
Show Gist options
  • Save jmbarbone/a0ebd0a281a98285b67f49db214afbd3 to your computer and use it in GitHub Desktop.
Save jmbarbone/a0ebd0a281a98285b67f49db214afbd3 to your computer and use it in GitHub Desktop.
base version of tibble::tribble()
datarows <- function(...) {
ls <- list(...)
w <- which(vapply(ls, inherits, NA, "formula"))
n <- length(w)
cols <- as.character(ls[w])
cols <- substr(cols, 2, nchar(cols))
ls <- ls[-w]
sa <- seq_along(ls)
sa <- (sa - 1) %% n
res <- lapply(split(ls, sa), unlist)
names(res) <- cols
as.data.frame(res, check.names = FALSE)
}
datarows(
~one, ~two, ~three,
1, 2, 3,
4, 5, 6
)
#> one two three
#> 1 1 2 3
#> 2 4 5 6
str(datarows(
~one, ~two, ~three,
"a", 2, 3,
"b", 5, 6
))
#> 'data.frame': 2 obs. of 3 variables:
#> $ one : chr "a" "b"
#> $ two : num 2 5
#> $ three: num 3 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment