Coder Social home page Coder Social logo

Comments (5)

TSchiefer avatar TSchiefer commented on August 18, 2024

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.

sound118 avatar sound118 commented on August 18, 2024

Awesome, that works!

from dm.

sound118 avatar sound118 commented on August 18, 2024

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.

sound118 avatar sound118 commented on August 18, 2024

@TSchiefer hi, any suggestion for above raised issue.?

from dm.

TSchiefer avatar TSchiefer commented on August 18, 2024

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)

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.