Basic sample function returns unused arguments

I am taking a stats class and we are doing some pretty basic things in R. I just reinstalled R+RStudio because my sample function wasn't working correctly and it still fails after a full reinstallation. Here is the code and error:

dice <- sample(x=6, size=10, replace=TRUE)
Error in sample(x = 6, size = 10, replace = TRUE) : 
  unused arguments (x = 6, size = 10, replace = TRUE)

It should be a simple line of code that "rolls" a dice (the values being 1-6) and does it 10 times. Does anyone know why it won't take any of the arguments?

Under the "Help" tab after I type ?sample it tells me:
sample(x, size, replace = FALSE, prob = NULL) are the necessary requirements - I'm using them yet still get the unused arguments error. What do I do?

That's very strange.

From copying and pasting the code above I get (note: I show the dice object at the end, just so you can see that it runs):

dice <- sample(x=6, size=10, replace=TRUE)

dice
#>  [1] 6 4 2 4 2 5 4 1 6 6

Created on 2018-09-11 by the reprex package (v0.2.0.9000).

Does this work for you in vanilla R, but not in RStudio? (see FAQ below for disambiguation)

Hi Mara, thanks for your quick response!

Upon inputting the code into vanilla R, it still gives me the same exact error, so it shouldn't be an issue with RStudio, but R itself.

I found that this line of code works:

sample.int(6, size=10, replace=TRUE)

However just sample() does not work - this is frustrating because this should allow me to input more values than just sample.int()

I'm at a total loss as to why that would be (I believe you, I just have no idea why).

Obviously you want your local install to work (who wouldn't?!) but I've tested it in a fresh rstudio.cloud workspace, and sample() works fine.

What happens if you specify the namespace? I have no idea why you would have something local overriding the sample() function, but, if so, this should help. (You could also test this by running a reprex).

dice <- base::sample(x=6, size=10, replace=TRUE)

Hi Mara, the line of code you provided worked just fine. Taking the "base::" out of the code makes it so R cannot read the argument, however with it then it works. Do I need to just use base::sample()
every time I use the sample function now?

If the line works with base::sample() but not with sample(), it means that you have some other package loaded that also has a function called sample() (or you wrote and loaded a function with this name) which is taking precedence over base::sample().

Some avenues to investigate:

  • Have you restarted your R session recently?
  • Are you automatically saving and loading your environment via an .Rdata file? (and therefore reloading functions you forgot about)
  • Do you have statements in an .RProfile file that are loading packages or sourcing scripts?
1 Like

Hi Jcblum,

Whenever I shut down R it asks me to save my data so I don't think it's automatically saving my environment within an .Rdata file. How do I check if I have any statements in an .RProfile file that are loading packages or sourcing scripts?

When R asks, do you always say no? If it's asking at all, then it may also be looking for an .RData file in your working directory when starting up, and may load it if one is found (even if it's from a long time ago).

To turn off this behavior completely from RStudio, see here: https://support.rstudio.com/hc/en-us/articles/200549016-Customizing-RStudio#general You want to uncheck "Restore .RData into workspace at startup". If you are using RStudio Projects, then you may also need to set this option separately in the Project Options.

This section of Efficient R Programming has a great explanation of .RProfile files (and other topics related to what R does while starting up): https://csgillespie.github.io/efficientR/set-up.html#r-startup
(it also explains how to prevent R from restoring the workspace from an .RData file when running R outside of RStudio)

1 Like