Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

features method failing with .var being a character string #385

Closed
leonfernandes opened this issue Jul 21, 2023 · 1 comment
Closed

features method failing with .var being a character string #385

leonfernandes opened this issue Jul 21, 2023 · 1 comment

Comments

@leonfernandes
Copy link

As demonstrated in the reprex below, the features.tbl_ts() fails when .var is a character string. Wrapping this function with a variable column name is a problem.

library(fabletools)
library(tsibble)
#> 
#> Attaching package: 'tsibble'
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, union
head(tourism, 160) |>
    features(Trips, features = list(mean = mean, sd = sd))
#> # A tibble: 2 × 5
#>   Region   State           Purpose   mean    sd
#>   <chr>    <chr>           <chr>    <dbl> <dbl>
#> 1 Adelaide South Australia Business  156.  35.6
#> 2 Adelaide South Australia Holiday   157.  27.1
head(tourism, 160) |>
    features("Trips", features = list(mean = mean, sd = sd))
#> Warning in mean.default(...): argument is not numeric or logical: returning NA

#> Warning in mean.default(...): argument is not numeric or logical: returning NA
#> Warning in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm =
#> na.rm): NAs introduced by coercion

#> Warning in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm =
#> na.rm): NAs introduced by coercion
#> # A tibble: 2 × 5
#>   Region   State           Purpose   mean    sd
#>   <chr>    <chr>           <chr>    <dbl> <dbl>
#> 1 Adelaide South Australia Business    NA    NA
#> 2 Adelaide South Australia Holiday     NA    NA
foo <- function(df, col_name) {
    df |>
        features(!!rlang::enquo(col_name), features = list(mean = mean, sd = sd))
}
head(tourism, 160) |>
    foo(Trips)
#> # A tibble: 2 × 5
#>   Region   State           Purpose   mean    sd
#>   <chr>    <chr>           <chr>    <dbl> <dbl>
#> 1 Adelaide South Australia Business  156.  35.6
#> 2 Adelaide South Australia Holiday   157.  27.1
head(tourism, 160) |>
    foo("Trips")
#> Warning in mean.default(...): argument is not numeric or logical: returning NA
#> Warning in mean.default(...): argument is not numeric or logical: returning NA
#> Warning in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm =
#> na.rm): NAs introduced by coercion

#> Warning in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm =
#> na.rm): NAs introduced by coercion
#> # A tibble: 2 × 5
#>   Region   State           Purpose   mean    sd
#>   <chr>    <chr>           <chr>    <dbl> <dbl>
#> 1 Adelaide South Australia Business    NA    NA
#> 2 Adelaide South Australia Holiday     NA    NA

Created on 2023-07-21 with reprex v2.0.2

@mitchelloharawild
Copy link
Member

features() allows for calculation of features from arbitrary expressions, and so "Trips" is handled as a length 1 character vector.
To select columns with tidyselect you can use features_at().

E.g.

library(fabletools)
library(tsibble)
#> 
#> Attaching package: 'tsibble'
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, union
head(tourism, 160) |>
  features(Trips, features = list(mean = mean, sd = sd))
#> # A tibble: 2 x 5
#>   Region   State           Purpose   mean    sd
#>   <chr>    <chr>           <chr>    <dbl> <dbl>
#> 1 Adelaide South Australia Business  156.  35.6
#> 2 Adelaide South Australia Holiday   157.  27.1
head(tourism, 160) |>
  features_at("Trips", features = list(mean = mean, sd = sd))
#> # A tibble: 2 x 5
#>   Region   State           Purpose  Trips_mean Trips_sd
#>   <chr>    <chr>           <chr>         <dbl>    <dbl>
#> 1 Adelaide South Australia Business       156.     35.6
#> 2 Adelaide South Australia Holiday        157.     27.1

Created on 2024-03-02 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants