What is the difference between the functions ls()
and objects()
?
I tried the following code and they give same result
a <- 1:10
b <- letters
c <- month.abb
Result:
> ls()
[1] "a" "b" "c"
> objects()
[1] "a" "b" "c"
ls() function in R Language is used to list the names of all the objects that are present in the working directory.
ls is standing for listing directories and files under a directory. In your situation, ls (without a directory argument) is going to list directories and files under the current directory(pwd). The other command, ls / is going to list files and directories under the root directory which is / .
The ls() code lists all of the objects in your workspace. The rm() code removes objects in your workspace. You can begin your code with the rm() function to clear all of the objects from your workspace to start with a clean environment.
In computing, ls is a command to list computer files in Unix and Unix-like operating systems. ls is specified by POSIX and the Single UNIX Specification. When invoked without any arguments, ls lists the files in the current working directory. The command is also available in the EFI shell.
They are identical. Looking at the source code they're literally just different names for the same code as can been seen here: https://github.com/wch/r-source/blob/bfe73ecd848198cb9b68427cec7e70c40f96bd72/src/library/base/R/attach.R#L200
The relevant snippet:
ls <- objects <-
function (name, pos = -1L, envir = as.environment(pos), all.names = FALSE,
pattern, sorted = TRUE)
{
We can also check that they have identical code from within R
> all.equal(body(objects), body(ls))
[1] TRUE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With