Jonathan L S
Jonathan L S

Reputation: 53

Unable to Replicate "R for Beginners" Example

I've searched around, but can't figure this one out guys. I'm a total noob to R and am learning from scratch through "R for Beginners"

Barely into it, pg. 6, I cannot replicate their sample. In trying to explain "max.level", they show...

> name <- "Carmen"; n1 <- 10; n2 <- 100; m <- 0.5
> M <- data.frame(n1, n2, m)

> ls.str(pat = "M")
M : ‘data.frame’: 1 obs. of 3 variables:
$ n1: num 10
$ n2: num 100
$ m : num 0.5

> ls.str(pat="M", max.level=-1)
M : ‘data.frame’: 1 obs. of 3 variables:

but in R I get...

> name <- "Carmen"; n1 <- 10; n2 <- 100; m <- 0.5
> M <- data.frame(n1, n2, m)

> ls.str(pat = "M")
M : 'data.frame':       1 obs. of  3 variables:
 $ n1: num 10
 $ n2: num 100
 $ m : num 0.5

> ls.str(pat="M", max.level=-1)
Error in ls.str(pat = "M", max.level = -1) : 
  unused argument (max.level = -1)

I have no idea what i did wrong or how to fix it. Is there a typo in the guide? Is there some library I haven't loaded properly?

Help much appreciated!

Upvotes: 5

Views: 944

Answers (2)

Les
Les

Reputation: 10605

Barely into it, pg. 6, I cannot replicate their sample. In trying to explain "max.level", they show...

Yeh. Here it is Jan 2021, and just as noob as you once were, I run into the same problem with the "R for Beginners" example. However, I did the "right thing" and consulted the official R documentation on CRAN-R.project.org. Here is the snip from current (at the time) reference manual (2020-1010).

Arguments
...
max.level     maximal level of nesting which is applied for displaying nested structures, e.g.,
              a list containing sub lists. Default 1: Display only the first nested level.
...

Ah! But don't let that fool you. Though it is listed as an argument, it is NOT an argument to ls.str(...). You have to look at the Usage section instead. It does not show max.level as an arguement for either ls.str() or lsf.str(), but does show it for print().

I use bold for argument because if I read the "2005 R for Beginners", the author makes a clear distinction between arguments and options and calls max.level an option (page 3, section 2.1, "How R Works"). I'm sorry, but I don't yet know if this distinction is important or not.

The point of my answer is this. If you are new to R, "R for Beginners" is the (currently) recommended primer, but don't expect accuracy, it is (apparently) not maintained. Instead, find the "reference" manual and read the full synopsis for a function (like ls.str() for example). Get use to the semantics of the documentation.

(Bonus)

If you are a programmer familiar with other languages, the R Reference Manual might be a little different than you'd expect. (Like for me, why did they put print() under ls.str() instead of just referencing the print() section? That way max.level wouldn't be under ls.str() at all. Is it because print() semantics for the ls_str object is special? Then why not link to the ls_str object and discuss the peculiarities there?). Get used to these kind of differences as you learn more.

Upvotes: 0

Zheyuan Li
Zheyuan Li

Reputation: 73315

Not surprising. The reference you use is in 2005. R has changed (a lot!!!). There is no longer an argument max.level for function ls.str. I suggest you go for ?ls.str to catch up for the update.

If you want examples, check the bottom of that help page.

You really should use the latest R documentation at https://cran.r-project.org/. This is kept up to date. The "introduction to R" is pretty good for starters, with moderate length. Have fun!


update

A quick way to check what arguments a function has is to use function args. For example, args(ls.str).

The error message from R is very informative. So whenever you see "unused arguments", you should check whether you have passed arguments correctly.

I believe in 2005, R is still in version R-2.**. Because when I picked up R at 2011, it was still R-2.14.**. But now R is in R-3.**. From version 2** to version 3**, R kernel has changed a lot.

Upvotes: 6

Related Questions