I am getting an error when I trying to use the str function in R.
Here is the data:
> dput(data1)
data1<- structure(list(Year = 1990:2010, Counts = c(1401L, 1290L, 1168L,1260L, 1252L, 1381L, 1330L, 1275L, 1235L, 1248L, 1237L, 1206L, 1175L, 1172L, 1055L, 1030L, 1002L, 987L, 915L, 806L, 868L)), .Names = c("Year", "Counts"), class = "data.frame", row.names = c(NA, -21L))
I do not know why R is giving me the following error with str while class function is clearly saying that data1 is a data.frame.
> class(data1)
[1] "data.frame"
> str(data1)
Error in str(data1) :
unable to find a non-generic version of function "str"
I am using R version 2.15.0 (2012-03-30) for Windows.
Any help will be greatly appreciated.
EDIT:
With a clean R session it worked. But why does this kind of problem occur? Is it possible to know this?
Ok, I’ll bite.
Why does this kind of problem occur?
Object (i.e. function, variable) masking is one of the most common sources of errors or bugs in R code. Why can this happen?
Well, there’s a list of reserved words in R’s parser located at
?Reserved:That means you, the user, can’t reassign the value of these names. But pretty much anything else (assuming its syntactically valid) is in bounds.
For example,
datais actually a function in the utils package, which is why people generally recommend not naming your data framedata. You will have masked the functiondataand any code that depends on it will choke.Honestly, I commonly use
dffor a data frame, even though that is a function that computes the distribution function for an F distribution. (I don’t need the F distribution functions much. But I still shouldn’t do that.)A particularly devious example of this issue is the common joke among R users about setting:
Many people are lazy and use
TandFforTRUEandFALSE, and now we see exactly why this is a really, really bad idea. (It’s pretty likely that someone is going to assign some other value toTorF.)Indeed, anytime you load a package you’re likely to see a bunch of warnings, where R is telling you that by loading that package you’ve just masked a bunch of functions because they have common names.
How can I know that this has happened?
This is much easier. As Ben said in the comments, you identify this sort of thing using
conflicts().