Reputation:
I have used installed.packages()
to see installed packages in R. And I want to extract two columns from the output, "Packages" and "Version" by using installed.packages()[c("Package","Version")]
but it does not show what I expect. But if I put a "," just before "Package" (installed.packages()[,c("Package","Version")])
it works! Why is a "," necessary in the statement?
Upvotes: 2
Views: 214
Reputation: 322
For future references, if you want to know which exact GitHub commit is used for a GitHub package:
install.packages("devtools")
library("devtools")
package_info("<package_name>")
Upvotes: 0
Reputation: 44525
You need to give ? Extract
a look to understand indexing in R. Here are some hints for how to understand what the object you're looking at is structured:
> class(installed.packages())
[1] "matrix"
> dim(installed.packages())
[1] 173 16
> str(installed.packages())
chr [1:173, 1:16] "aws.s3" "aws.signature" "BH" "bit" "bit64" ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:173] "aws.s3" "aws.signature" "BH" "bit" ...
..$ : chr [1:16] "Package" "LibPath" "Version" "Priority" ...
So, what that tells us is that the object is a matrix, with 173 rows and 16 columns.
matrix[rows, columns]
.matrix[, columns]
.matrix[rows,]
You're probably expecting that the object is a data.frame instead. A data.frame allows various other forms of indexing/extraction with which you might be more familiar:
> str(mtcars["mpg"])
'data.frame': 32 obs. of 1 variable:
$ mpg: num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
> str(mtcars[["mpg"]])
num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
> str(mtcars[, "mpg"])
num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
> str(mtcars$mpg)
num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
Upvotes: 2
Reputation: 1316
The contents of installed.packages()
have columns and rows and in subsetting [a, b]
, a
are rows and b
are columns. You're asking for the columns named 'package' and 'version', so you need to tell it to look at columns for those names.
More guidance here: http://statmethods.net/management/subset.html and http://adv-r.had.co.nz/Subsetting.html.
Upvotes: 3