w3resource

R Program to get and display details of objects in memory

R Programming: Basic Exercise-2 with Solution

Write a R program to get the details of the objects in memory.

Sample Solution :

R Programming Code :

# Assign the string "Python" to the variable `name`
name = "Python"

# Assign the integer value 10 to the variable `n1`
n1 =  10

# Assign the numeric value 0.5 to the variable `n2`
n2 =  0.5

# Create a vector `nums` containing the numbers 10, 20, 30, 40, 50, 60
nums = c(10, 20, 30, 40, 50, 60)

# Print the names of all objects currently in the environment
print(ls())

# Print a message indicating that details of the objects in memory will be shown
print("Details of the objects in memory:")

# Print a detailed list of all objects in memory, including their structure
print(ls.str()) 

Output:

[1] "n1"   "n2"   "name" "nums"
[1] "Details of the objects in memory:"
n1 :  num 10
n2 :  num 0.5
name :  chr "Python"
nums :  num [1:6] 10 20 30 40 50 60                         

Explanation:

  • Define Variables:
    • name = "Python": Assigns the string "Python" to the variable name.
    • n1 = 10: Assigns the integer 10 to the variable n1.
    • n2 = 0.5: Assigns the floating-point number 0.5 to the variable n2.
    • nums = c(10, 20, 30, 40, 50, 60): Creates a vector named nums containing the numbers 10, 20, 30, 40, 50, 60.
  • List All Objects in Memory:
    • print(ls()): Displays the names of all objects currently stored in memory (i.e., name, n1, n2, nums).
  • Print Object Details:
    • print("Details of the objects in memory:"): Prints a descriptive message.
    • print(ls.str()): Displays detailed information about each object in memory, such as their types and contents.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to take input from the user (name and age) and display the values. Also print the version of R installation.
Next: Write a R program to create a sequence of numbers from 20 to 50 and find the mean of numbers from 20 to 60 and sum of numbers from 51 to 91.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/r-programming-exercises/basic/r-programming-basic-exercise-2.php