Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ls() and objects()

Tags:

r

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"
like image 288
useR Avatar asked Apr 13 '15 10:04

useR


People also ask

Why ls () is used in R?

ls() function in R Language is used to list the names of all the objects that are present in the working directory.

What is the difference in ls and ls in terminal?

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 / .

What is RM list ls ()) in R?

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.

What are the functions of ls?

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.


1 Answers

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
like image 77
Dason Avatar answered Sep 26 '22 06:09

Dason