Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does accessing zero element in R do?

Tags:

r

if I have a vector a<-c(3, 5, 7, 8)

and run a[1], not surprisingly I will get 3 but if I will run a[0] I basically get numeric(0)

What does this mean? And what does this do? How can I use it for normal reasons?


1 Answers

Others have answered what x[0] does, so I thought I'd expand on why it's useful: generating test cases. It's great for making sure that your functions work with unusual data structure variants that users sometimes produce accidentally.

For example, it makes it easy to generate 0 row and 0 column data frames:

mtcars[0, ]
mtcars[, 0]

These can arise when subsetting goes wrong:

mtcars[mtcars$cyl > 10, ]

But in your testing code it's useful to flag that you're doing it deliberately.

like image 190
hadley Avatar answered Sep 11 '26 06:09

hadley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!