RStudio simple practical exercises BS 2020-june -09 Flashcards
aaaa
b
bb
rarar
valalalala
Create Multiplication Table
R Program to find the multiplicationtable (from 1 to 10)
take input from the user
num = as.integer(readline(prompt = “Enter a number: “))
use for loop to iterate 10 times
for(i in 1:10) {
print(paste(num,’x’, i, ‘=’, num*i))
}
Output
Enter a number: 7
[1] “7 x 1 = 7”
[1] “7 x 2 = 14”
[1] “7 x 3 = 21”
[1] “7 x 4 = 28”
[1] “7 x 5 = 35”
[1] “7 x 6 = 42”
[1] “7 x 7 = 49”
[1] “7 x 8 = 56”
[1] “7 x 9 = 63”
[1] “7 x 10 = 70”
Here, we ask the user for a number which is stored in num variable.
Then, the for loop is iterated 10 times from i equals to 1 to i equals to 10. In each iteration, a row of the multiplication table is printed using:
print(paste(num,’x’, i, ‘=’, num*i))
Write a R program to take input from the user (name and age) and display the values. Also print the version of R installation.
name = readline(prompt=”Input your name: “)
age = readline(prompt=”Input your age: “)
print(paste(“My name is”,name, “and I am”,age ,”years old.”))
print(R.version.string)
Write a R program to get the details of the objects in memory.
name = “Python”;
n1 = 10;
n2 = 0.5
nums = c(10, 20, 30, 40, 50, 60)
print(ls())
print(“Details of the objects in memory:”)
print(ls.str())
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.
print(“Sequence of numbers from 20 to 50:”)
print(seq(20,50))
print(“Mean of numbers from 20 to 60:”)
print(mean(20:60))
print(“Sum of numbers from 51 to 91:”)
print(sum(51:91))
- Write a R program to create a vector which contains 10 random integer values between -50 and +50.
v = sample(-50:50, 10, replace=TRUE)
print(“Content of the vector:”)
print(“10 random integer values between -50 and +50:”)
print(v)
- Write a R program to get the first 10 Fibonacci numbers.
Fibonacci Fibonacci[1] for (i in 3:10) Fibonacci[i] print(“First 10 Fibonacci numbers:”)
print(Fibonacci)
Write a R program to get all prime numbers up to a given number (based on the sieve of Eratosthenes).
prime_numbers if (n >= 2) {
x = seq(2, n)
prime_nums = c()
for (i in seq(2, n)) {
if (any(x == i)) {
prime_nums = c(prime_nums, i)
x = c(x[(x %% i) != 0], i)
}
}
return(prime_nums)
}
else
{
stop(“Input number should be at least 2.”)
}
}
prime_numbers(12)
- Write a R program to print the numbers from 1 to 100 and print “Fizz” for multiples of 3, print “Buzz” for multiples of 5, and print “FizzBuzz” for multiples of both.
for (n in 1:100) {
if (n %% 3 == 0 & n %% 5 == 0) {print(“FizzBuzz”)}
else if (n %% 3 == 0) {print(“Fizz”)}
else if (n %% 5 == 0) {print(“Buzz”)}
else print(n)
}
- Write a R program to extract first 10 english letter in lower case and last 10 letters in upper case and extract letters between 22nd to 24th letters in upper case.
print(“First 10 letters in lower case:”)
t = head(letters, 10)
print(t)
print(“Last 10 letters in upper case:”)
t = tail(LETTERS, 10)
print(t)
print(“Letters between 22nd to 24th letters in upper case:”)
e = tail(LETTERS[22:24])
print(e)
- Write a R program to find the factors of a given number.
factors: lenyegeben amivel oszthato (factors of 4: 4, 2, 1)
print_factors = function(n) {
print(paste(“The factors of”,n,”are:”))
for(i in 1:n) {
if((n %% i) == 0) {
print(i)
}
}
}
print_factors(4)
print_factors(7)
print_factors(12)
Sample Output:
[1] “The factors of 4 are:”
[1] 1
[1] 2
[1] 4
[1] “The factors of 7 are:”
[1] 1
[1] 7
[1] “The factors of 12 are:”
[1] 1
[1] 2
[1] 3
[1] 4
[1] 6
[1] 12
- Write a R program to find the maximum and the minimum value of a given vector.
nums = c(10, 20, 30, 40, 50, 60)
print(‘Original vector:’)
print(nums)
print(paste(“Maximum value of the said vector:”,max(nums)))
print(paste(“Minimum value of the said vector:”,min(nums)))
Copy
Sample Output:
[1] “Original vector:”
[1] 10 20 30 40 50 60
[1] “Maximum value of the said vector: 60”
[1] “Minimum value of the said vector: 10”
- Write a R program to get the unique elements of a given string and unique numbers of vector.
str1 = “The quick brown fox jumps over the lazy dog.”
print(“Original vector(string)”)
print(str1)
print(“Unique elements of the said vector:”)
print(unique(tolower(str1)))
nums = c(1, 2, 2, 3, 4, 4, 5, 6)
print(“Original vector(number)”)
print(nums)
print(“Unique elements of the said vector:”)
print(unique(nums))
Copy
Sample Output:
[1] “Original vector(string)”
[1] “The quick brown fox jumps over the lazy dog.”
[1] “Unique elements of the said vector:”
[1] “the quick brown fox jumps over the lazy dog.”
[1] “Original vector(number)”
[1] 1 2 2 3 4 4 5 6
[1] “Unique elements of the said vector:”
[1] 1 2 3 4 5 6
- Write a R program to create three vectors a,b,c with 3 integers. Combine the three vectors to become a 3×3 matrix where each column represents a vector. Print the content of the matrix.
abcmprint(“Content of the said matrix:”)
print(m)
Copy
Sample Output:
[1] “Content of the said matrix:”
a b c
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9