Names of R's available packages
Asked Answered
P

4

19

I'm eager to know,

  • how many package names on CRAN have two, three, N characters?
  • which combinations have not yet been used ("unpoppler")
  • how many package names use full-caps, or camelCase?
  • how many package names end in 2?

I think it might reveal some interesting facts.

Edit: bonus points for animated graphics showing the time-evolution of CRAN packages.

Posner answered 11/9, 2011 at 23:14 Comment(5)
It's an interesting question, but I'm not sure if it's really SO-style. Should just be a matter of scraping the names off cran.r-project.org/web/packages/available_packages_by_name.html and running a few regexes on them, though.Packet
@Packet I agree on both points, but curiosity's sake won me over.Posner
Scraping is overkill: just look at myList[,"Package"] where myList <- available.packages(). This list is subject to change every day.Crowley
As for the time evolution, here is a database with API: github.com/metacran/crandb (Disclaimer: I am the author of it.) It has some incorrect data, in particular dates of archivals are often wrong. Some of these I can and will fix, but for some, there is just no information available AFAIK.Lessen
you're doing an outstanding work with metacranPosner
C
14

A better way than scraping a web page to get the names of packages is to use the available.packages() function and process those results. available.packages() returns a matrix contains details of all packages available (but is filtered by default — see the Details section of ?available.packages for more).

pkgs <- available.packages(filters = "duplicates")
nameCount <- unname(nchar(pkgs[, "Package"]))
table(nameCount)

> table(nameCount)
nameCount
  2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21 
 32 311 374 360 434 445 368 277 199 132  99  56  56  43  22  19  18   2  12   8 
 22  24  25  31 
  5   2   1   1

Using nameCount we can select packages with names containing any number of characters without needing to resort to regexp etc:

> unname(pkgs[which(nameCount == 2), "Package"])
 [1] "BB" "bs" "ca" "cg" "dr" "ez" "FD" "ff" "HH" "HI" "iv" "JM" "ks" "M3" "mi"
[16] "np" "oc" "oz" "PK" "PP" "qp" "QT" "RC" "rv" "Rz" "sm" "sn" "sp" "st" "SV"
[31] "tm" "wq"
Clang answered 12/9, 2011 at 9:45 Comment(0)
P
10

here's one shot based on various suggestions.

 packages <- available.packages()[,'Package']

 ggplot(data.frame(n = nchar(packages))) +
   geom_histogram(aes(n), binwidth=1)     

histogram

 all <- length(packages)
 ## 3168
 up <- sum(toupper(packages) == packages)
 ## 262
 low <- sum(tolower(packages) == packages)
 ## 1697
 pie(c(up, low, all-up-low), labels=c("UPPERCASE","lowercase","cAmElCaSe"))

pie

 let <- sapply(sapply(letters, grep, tolower(packages)), length)
 barplot(let)

barplot

 length(packages[grep("2$", packages, perl=TRUE)])
 # 29
Posner answered 12/9, 2011 at 9:45 Comment(2)
Also interesting: how many packages contain dots? packages[grepl("\\.", packages)]Parous
In your pie chart, "cAmElCaSe" also includes those with only the first letter capitalized, which I don't think of as CamelCase.Alcaide
L
5

Here is a short piece of code to answer some questions. I will keep adding to my answer when I find time.

library(XML); library(ggplot2);

url = 'http://cran.r-project.org/web/packages/available_packages_by_name.html'
packages = readHTMLTable(url, stringsAsFactors = F)[[1]][-1,]

# histogram of number of characters in package name
qplot(nchar(V1), data = packages)
Lamprophyre answered 11/9, 2011 at 23:43 Comment(3)
Nice, RthroughExcelWorkbooksInstaller2 anyone? :)Posner
+1, though this might slightly overstate the number with two characters as it is picking up some NA values (e.g. packages[128,]) when the first letter changes, perhaps from html section names.Rutherfordium
yes. but i think the available.packages solution is more elegant and robust.Lamprophyre
B
1

Make a vector of all the packages using

myList <- available.packages()[,'Package']

Then you can analyze however you want. For example, a list of packages with just two character names

myList[grep('^..$', myList)]
Brazier answered 12/9, 2011 at 9:5 Comment(2)
nchar is faster and more readable for counting the number of characters in a string. myList[nchar(myList) == 2]Parous
Also, use grepl rather than grep for indexing. If you have an empty match, grep doesn't do what you think it does.Parous

© 2022 - 2024 — McMap. All rights reserved.