Comments (5)
Thanks, your example would work, if the altered table was used in the dm. Actually, in the function dm_nycflights13()
, the flights
table is supplied via nycflights13::flights
(you can check the function definition by calling dm_nycflights13
without braces in the console), so it will always use the table as it is defined in the {nycflights13} package.
You could make it work here like this:
library(dm)
as_timestamp_without_timezone <- function(x) {
class(x) <- "timestamp without timezone"
x
}
nyc_dm <- dm_nycflights13() %>%
dm_zoom_to(flights) %>%
mutate(dep_time = as_timestamp_without_timezone(dep_time)) %>%
dm_update_zoomed()
sapply(nyc_dm$flights, class)
#> $year
#> [1] "integer"
#>
#> $month
#> [1] "integer"
#>
#> $day
#> [1] "integer"
#>
#> $dep_time
#> [1] "timestamp without timezone"
#>
#> $sched_dep_time
#> [1] "integer"
#>
#> $dep_delay
#> [1] "numeric"
#>
#> $arr_time
#> [1] "integer"
#>
#> $sched_arr_time
#> [1] "integer"
#>
#> $arr_delay
#> [1] "numeric"
#>
#> $carrier
#> [1] "character"
#>
#> $flight
#> [1] "integer"
#>
#> $tailnum
#> [1] "character"
#>
#> $origin
#> [1] "character"
#>
#> $dest
#> [1] "character"
#>
#> $air_time
#> [1] "numeric"
#>
#> $distance
#> [1] "numeric"
#>
#> $hour
#> [1] "numeric"
#>
#> $minute
#> [1] "numeric"
#>
#> $time_hour
#> [1] "POSIXct" "POSIXt"
Created on 2022-11-29 by the reprex package (v2.0.1)
from dm.
Awesome, that works!
from dm.
Sorry, I still have a puzzle related to this column type issue. Let's take below minimal example:
df1 <- data.frame(
id = c('122','345', '43'),
name = c('john','matt','roger'),
race = c('1','2','1'),
age = c('20','23','34'),
height = c('6.4', '5.7', '4.9'),
stringsAsFactors = FALSE
)
cols <- data.frame(
name = c('id','name', 'race', 'age', 'height'),
type = c('varchar(20)', 'varchar(200)', 'varchar(10)', 'int', 'numeric(3,1)'),
stringsAsFactors = FALSE)
I actually want to convert all column types in DM object into the type in "cols" data frame. I don't want to do this with mutate for each column one by one which is some sort of hard coding. Instead, I am wondering if we could do as following duck-typed script:
dm_obj <- dm(df1)
convert_col_type <- function(df) {
for (i in seq_len(length(colnames(df)))) {
class(df[[colnames(df)[i]]]) <- cols$type[i]
}
df
}
test_dm <- dm_obj %>%
dm_zoom_to(df1) %>%
convert_col_type() %>%
dm_update_zoomed()
Above scrip obviously has some error issue. But if we simply run str(convert_col_type(df1)), it could show us the converted column types. Just wondering how we could incorporate this into "dm" package framework. Thanks a lot in advance!
from dm.
@TSchiefer hi, any suggestion for above raised issue.?
from dm.
Sorry for the late answer.
I generally think, not everything needs to be done within a dm
object. You could work around doing the following:
suppressPackageStartupMessages({
library(tibble)
library(purrr)
library(dplyr)
library(dm)
})
df1 <- data.frame(
id = c('122','345', '43'),
name = c('john','matt','roger'),
race = c('1','2','1'),
age = c('20','23','34'),
height = c('6.4', '5.7', '4.9'),
stringsAsFactors = FALSE
)
cols <- data.frame(
name = c('id','name', 'race', 'age', 'height'),
type = c('varchar(20)', 'varchar(200)', 'varchar(10)', 'int', 'numeric(3,1)'),
stringsAsFactors = FALSE) %>%
deframe()
# now a named character vector
cols
#> id name race age height
#> "varchar(20)" "varchar(200)" "varchar(10)" "int" "numeric(3,1)"
df2 <- imap(df1, function(col, colname) {
class(col) <- cols[[colname]]
col
}) %>%
bind_cols()
df2
#> # A tibble: 3 × 5
#> id name race age height
#> <vrch(20)> <vrc(200)> <vrch(10)> <int> <nmr(3,1)>
#> 1 122 john 1 20 6.4
#> 2 345 matt 2 23 5.7
#> 3 43 roger 1 34 4.9
map_chr(df2, class)
#> id name race age height
#> "varchar(20)" "varchar(200)" "varchar(10)" "int" "numeric(3,1)"
dm(df2)
#> ── Metadata ────────────────────────────────────────────────────────────────────
#> Tables: `df2`
#> Columns: 5
#> Primary keys: 0
#> Foreign keys: 0
Created on 2022-12-20 by the reprex package (v2.0.1)
from dm.
Related Issues (20)
- Investigate yyjson
- `copy_dm_to()` issues on Oracle HOT 1
- Do we need `dm_cast()` ? HOT 1
- Silencing dm functions optionally HOT 1
- Make igraph dependency optional HOT 5
- Fix SQL Server CI/CD
- Examples in "Getting Started" vignette are broken HOT 2
- Action Required: Fix Renovate Configuration
- Error in example in "Cheatsheet" vignette : pull_tbl()
- dm incompatible with pkgdown in a package that imports dm
- Title in Documentation
- `as_dm()` accepts non-dataframe objects which can lead to unexpected errors
- Full list of problematic values from `dm_examine_constraints()` HOT 1
- Non existing remote
- How to modify keys of a dm from connection?
- Learning from SQL Server
- Disable failing DuckDB-related test-cases on Big-endian platforms (indices and sorting are broken in DuckDB upstream)
- Review starschemar
- Naming pattern not used in `dm_from_con()` when keys are learned successfully
- New `dm_add_rk()` (referencing key?) HOT 1
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 dm.