Getting Started with R | R Language Practical Slip With Solution - letsbug
In this article we are solving some R programming questions that might get asked in an exam. These examples are simple and after solving these one will have a better understanding of R language. But before we start make sure you have a IDE or a text editor where you can run your R program. You should also have R language installed on your system for it to work.
Now let's start fire up you code editor. I am using VS code for R, not necessary you can have R Studio. Now let's start
R language practicals
1. R program to find the maximum and the minimum value of a given vector.
code:
x = c(10,20,30,25,9, 36)print("Original Value")print(x)print("Maximum value of the above Vector")print(max(x))print("Minimum value of the above Vector")print(min(x))
output:
[1] 10 20 30 25 9 36
[1] "Maximum value of the above Vector"
[1] 36
[1] "Minimum value of the above Vector"
[1] 9
2. R program to sort a vector in ascending and descending order
x = c(10,20,03,030, 24,9,35)print("Original Value:")print(x)print("Sorting in ascending order:")print(sort(x))print("Sorting in descending order:")print(sort(x, decreasing=TRUE))
output:
3. R program to compare two data frames to find the elements in first dataframe that are not present in second dataframe.
a = c("a", "b", "c", "d", "e")b = c("d", "e", "f", "g")print("Original Dataframes")print(a)print(b)print("Data in first dataframe that are not present in second dataframe:")result = setdiff(a, b)print(result)
output:
4. R program to extract first 10 english letter in lowercase and last 10 letters in upper case and extract letters between 22nd and 24th letters in upper case.
print("First 10 letters in lowercase")t = head(letters, 10)print(t)print("Last 10 letters in Uppercase")t = tail(LETTERS, 10)print(t)print("Letters between 22nd to 24th in uppercase")e = tail(LETTERS[22:24])print(e)
output:
5. R program to create a simple bar plot of five subject's marks.
marks = c(70, 95, 80, 74)barplot(marks,main = "Comparing Marks Of 5 Subjects",xlab = "Subject",ylab = "Marks",names.arg = c("English", "Science", "Math", "History"),col = "blue",horiz = FALSE)
output:
output |
6. R program to find sum, mean and product of a vector.
x = c(10,20,30)print("Sum:")print(sum(x))print("Mean:")print(mean(x))print("Product:")print(prod(x))
output:
7. R program to create a data frames which contain details of 5 employees and display the details in ascending order.
Employees = data.frame(Name=c("Aniket", "Swastik", "Suyash", "Pranav", "Ibrahim"),Gender=c("M", "M", "M", "M", "M"),Age=c(23,22,25,26, 32),Designation=c("CEO", "Manager", "Exective", "HR", "Assistant"),SSN=c("123-34-2346", "123-44-779", "556-24-433", "123-09-987", "679-77-576"))print("Details of the employees: ")print(Employees)
output:
8. R program to create a data frame using two given vectors and display the duplicated elements and unique rows of the said data frame.
a = c(10,20,10,10,40,50,20,30)b = c(10,30,10,20,0,50,30,30)print("Original Dataframe: ")ab = data.frame(a,b)print(ab)print("Duplicate elements of the said data frame:")print(duplicated(ab))print("Unique row of the said dataframe:")print(unique(ab))
output:
9. R program to change the first level of a factor with another level of a given factor.
v = c("a", "b", "a", "c", "b")print("Original vector:")print(v)f = factor(v)print("Factor of the said vector: ")print(f)levels(f)[1] = "e"print(f)
output:
10. R program to create two vectors of different lengths and give these vectors as input to array and print addition and subtraction of those matrices.
print("Two vectors of different lengths: ")v1 = c(1,3,4,5)v2 = c(10,11,13,12,14,15)print(v1)print(v2)result = array(c(v1, v2),dim = c(3,3,2))print("New Array: ")print(result)print("The second row of the second matrix of the array: ")print(result[2,,2])print("The element in the 3rd row and 3rd column of the 1st matrix: ")print(result[3,3,1])
output:
Comments
Post a Comment