Dplyr data frame

Hi All

I am creating carsx using the following code.
My understanding is carsx will have all the columns from cars data frame (i.e speed and dist), so I am trying to access these columns using carsx$speed notation but R does not Like it and gives me a warning message
But when I use carsx$cars$speed notation , I was able to get the values that I need

Can anyone explain me this . I am little confused.

library(dplyr)
carsx <- filter(tibble(cars),cars$speed == 20)
carsx$speed
NULL
Warning message:
Unknown or uninitialised column: 'speed'.
carsx$cars$speed
[1] 20 20 20 20 20

Thanks

If your cars object is a data frame, there is no need to run it through the tibble() function. I get an error when I try that

Error: Column `cars` must be a 1d atomic vector or a list

Also, filter can take bare column names.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
data(cars)
head(cars)
#>   speed dist
#> 1     4    2
#> 2     4   10
#> 3     7    4
#> 4     7   22
#> 5     8   16
#> 6     9   10
carsx <- filter(cars,speed == 20)
range(carsx$speed)
#> [1] 20 20

Created on 2019-05-14 by the reprex package (v0.2.1)

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.