1

This is my first attempted reprex. I am working through R for Data Science. I am trying to narrow down a data frame to then be able to mutate it, but having trouble with the endsWith() function I think. When I run this section of the code I get the following error message. You can see that when I then change to (x, "delay") I get a different message. I am not sure how to deal with either and would love some help. Also, I'm not sure why, but dplyr::select() is working for me (as an example), while select() is not, so that's why it's different than the book. Thanks!

flights_sml <- dplyr::select(flights,
         year:day,
         endsWith("delay"),
         distance,
         air_time
       ) 

Error: argument "suffix" is missing, with no default Run rlang::last_error() to see where the error occurred.

flights_sml <- dplyr::select(flights,
         year:day,
         endsWith(x, "delay"),
         distance,
         air_time
       ) 

Error: Must subset columns with a valid subscript vector. x Subscript has the wrong type logical. ℹ It must be numeric or character.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
ksh530
  • 33
  • 4

1 Answers1

1

Good start. The tidy-select function is ends_with, while the base r function is endsWith

library(nycflights13)
library(dplyr)


flights_sml<-flights %>%
  select(year:day,ends_with("delay"),distance,air_time)

flights_sml
#> # A tibble: 336,776 × 7
#>     year month   day dep_delay arr_delay distance air_time
#>    <int> <int> <int>     <dbl>     <dbl>    <dbl>    <dbl>
#>  1  2013     1     1         2        11     1400      227
#>  2  2013     1     1         4        20     1416      227
#>  3  2013     1     1         2        33     1089      160
#>  4  2013     1     1        -1       -18     1576      183
#>  5  2013     1     1        -6       -25      762      116
#>  6  2013     1     1        -4        12      719      150
#>  7  2013     1     1        -5        19     1065      158
#>  8  2013     1     1        -3       -14      229       53
#>  9  2013     1     1        -3        -8      944      140
#> 10  2013     1     1        -2         8      733      138
#> # … with 336,766 more rows

Created on 2022-01-16 by the reprex package (v2.0.1)

Joe Erinjeri
  • 1,200
  • 1
  • 7
  • 15