w3resource

R Programming: Create Sequence, find Mean, and calculate Sum

R Programming: Basic Exercise-3 with Solution

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.

Sample Solution :

R Programming Code :

# Print a message indicating that the sequence of numbers from 20 to 50 will be shown
print("Sequence of numbers from 20 to 50:")

# Generate and print a sequence of numbers from 20 to 50
print(seq(20,50))

# Print a message indicating that the mean of numbers from 20 to 60 will be shown
print("Mean of numbers from 20 to 60:")

# Calculate and print the mean of numbers from 20 to 60
print(mean(20:60))

# Print a message indicating that the sum of numbers from 51 to 91 will be shown
print("Sum of numbers from 51 to 91:")

# Calculate and print the sum of numbers from 51 to 91
print(sum(51:91))

Output:

[1] "Sequence of numbers from 20 to 50:"
 [1] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
[26] 45 46 47 48 49 50
[1] "Mean of numbers from 20 to 60:"
[1] 40
[1] "Sum of numbers from 51 to 91:"
[1] 2911                         

Explanation:

  • print("Sequence of numbers from 20 to 50:")
    Prints a descriptive message indicating that the following output will be a sequence of numbers from 20 to 50.
  • print(seq(20,50))
    Creates and prints a sequence of numbers starting from 20 and ending at 50 using the seq() function.
  • print("Mean of numbers from 20 to 60:")
    Prints a message indicating that the next output will show the mean (average) of numbers from 20 to 60.
  • print(mean(20:60))
    Calculates and prints the mean (average) of the numbers in the range from 20 to 60 using the mean() function.
  • print("Sum of numbers from 51 to 91:")
    Prints a message indicating that the next output will show the sum of numbers from 51 to 91.
  • print(sum(51:91))
    Calculates and prints the sum of all numbers from 51 to 91 using the sum() function.

R Programming Code Editor:



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

Previous: Write a R program to get the details of the objects in memory.
Next: Write a R program to create a vector which contains 10 random integer values between -50 and +50.

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-3.php