2

I use package 'tidytuesdayR'

target<-tidytuesdayR::tt_load(2021,30)
df<-target$drought

df$map_date
df[,1]

What is the difference between df$map_date and df[,1] in terms of the output? What functions will you use to explain the differences between them?

Phil
  • 7,287
  • 3
  • 36
  • 66
heedol
  • 21
  • 2
  • I don't have the package installed, but assuming that `map_date` is the first variabble in the data frame, there is no difference between the two in terms of their output. They both extract a variable from a data frame and return a vector. – Phil Feb 21 '22 at 07:22
  • 2
    Adding to @Phil. Your `df` is a `tibble`, which is a kind of a `data.frame`. However, there are some differences between a `tibble` and a `data.frame`. When using `$` to extract a column you will get a vector in either case. However, extracting a column from a `tibble` via e.g. `df[,1]` the result will still be a `tibble`, while in case of a `data.frame` the result will by default be simplified to a vector. As a consequence, using `$` or `[]` will return different objects in case of a tibble and hence the output printed in the console will be different as well. – stefan Feb 21 '22 at 07:38
  • 2
    This link may be useful: https://adv-r.hadley.nz/subsetting.html – Peter Feb 21 '22 at 08:30
  • Related post: https://stackoverflow.com/q/1169456/680068 – zx8754 Feb 21 '22 at 08:44
  • Also related: https://stackoverflow.com/questions/45918827/difference-between-and-operators-for-subsetting - A detailed look at differences, but the question is more about the unpredictable nature of data.frame subsetting than the operators more generally. – Captain Hat Feb 21 '22 at 10:20

2 Answers2

0

x$var is a shorthand for x[["var"]]

There are two main differences:

1. $ cannot be used with a character variable

Eg:

### All examples are from Hadley Wichams _Advanced R_ - see link at end of answer.
var <- "cyl"
# Doesn't work - mtcars$var translated to mtcars[["var"]]
mtcars$var
#> NULL

# Instead use [[
mtcars[[var]]
#>  [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4

2. $ uses left-to-right partial matching

This makes it convenient for interactive use, but unpredictable. For this reason, many people use [[ rather than $ when writing function and scripts, and use $ for convenience whilst interacting directly with data.

Partial matching example:

x <- list(abc = 1)
x$a
#> [1] 1
x[["a"]]
#> NULL

Further reading

For more info on these operators, and a reasonably thorough look "under the hood" of the R engine, check out Hadley's Advanced R, which is available for free online. These examples are reproduced from his section on subsetting.

Captain Hat
  • 2,444
  • 1
  • 14
  • 31
0

$ is 1-dimension vector not including name of column [] is data.frame so including name of column

heedol
  • 21
  • 2
  • 1
    This isn't quite true: Data frames return a vector when you subset a single column, regardless of whether you subset using `$` or `[`. It's only possible to subset one column with `$`, so `$` _always_ returns a vector, whereas `[` sometimes returns a vector and sometimes returns a data.frame. – Captain Hat Feb 21 '22 at 16:01