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] "Original Value"
[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

code:
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:

[1] "Original Value:"
[1] 10 20  3 30 24  9 35
[1] "Sorting in ascending order:"
[1]  3  9 10 20 24 30 35
[1] "Sorting in descending order:"
[1] 35 30 24 20 10  9  3

3. R program to compare two data frames to find the elements in first dataframe that are not present in second dataframe.

code:
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:

[1] "Original Dataframes"
[1] "a" "b" "c" "d" "e"
[1] "d" "e" "f" "g"
[1] "Data in first dataframe that are not present in second dataframe:"
[1] "a" "b" "c"

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.

code:
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:

[1] "First 10 letters in lowercase"
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
[1] "Last 10 letters in Uppercase"
 [1] "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
[1] "Letters between 22nd to 24th in uppercase"
[1] "V" "W" "X"

5. R program to create a simple bar plot of five subject's marks.

code:
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:

bar plotting in R language - letsbug
output


6. R program to find sum, mean and product of a vector.

code:
x = c(10,20,30)
print("Sum:")
print(sum(x))
print("Mean:")
print(mean(x))
print("Product:")
print(prod(x))

output:

[1] "Sum:"
[1] 60
[1] "Mean:"
[1] 20
[1] "Product:"
[1] 6000

7. R program to create a data frames which contain details of 5 employees and display the details in ascending order.

code:
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:

[1] "Details of the employees: "
     Name Gender Age Designation         SSN
1  Aniket      M  23         CEO 123-34-2346
2 Swastik      M  22     Manager  123-44-779
3  Suyash      M  25    Exective  556-24-433
4  Pranav      M  26          HR  123-09-987
5 Ibrahim      M  32   Assistant  679-77-576

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.

code:
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:

[1] "Original Dataframe: "
   a  b
1 10 10
2 20 30
3 10 10
4 10 20
5 40  0
6 50 50
7 20 30
8 30 30
[1] "Duplicate elements of the said data frame:"
[1] FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
[1] "Unique row of the said dataframe:"
   a  b
1 10 10
2 20 30
4 10 20
5 40  0
6 50 50
8 30 30

9. R program to change the first level of a factor with another level of a given factor.

code:
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:

[1] "Original vector:"
[1] "a" "b" "a" "c" "b"
[1] "Factor of the said vector: "
[1] a b a c b
Levels: a b c
[1] e b e c b
Levels: e b c

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.

code:
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:

[1] "Two vectors of different lengths: "
[1] 1 3 4 5
[1] 10 11 13 12 14 15
[1] "New Array: "
, , 1

     [,1] [,2] [,3]
[1,]    1    5   13
[2,]    3   10   12
[3,]    4   11   14

, , 2

     [,1] [,2] [,3]
[1,]   15    4   11
[2,]    1    5   13
[3,]    3   10   12

[1] "The second row of the second matrix of the array: "
[1]  1  5 13
[1] "The element in the 3rd row and 3rd column of the 1st matrix: "
[1] 14


Thankyou.

Comments

Categories

Big Data Analytics Binary Search Binary Search Tree Binary To Decimal binary tree Breadth First Search Bubble sort C Programming c++ Chemical Reaction and equation class 10 class 10th Class 9 Climate Complex Numbers computer network counting sort CSS Cyber Offenses Cyber Security Cyberstalking Data Science Data Structures Decimal To Binary Development diamond pattern Digital Marketing dust of snow Economics Economics Lesson 4 Email Validation English fire and ice Food Security in India Footprints Without feet Forest And Wildlife Resources game Geography Geography lesson 6 glassmorphism Glossary Graph HackerRank Solution hindi HTML image previewer India-Size And Location Insertion Sort Internet Network Status Interview Questions Introduction to cyber crime and cyber security IT javascript tricks json to CSV converter lesson 2 lesson 1 lesson 2 Lesson 3 Lesson 6 lesson 7 Life lines of National Economy life processes Linear Search Linked List lowest common ancestor Machine Learning MCQs median in array Merge sort min and max of two numbers Moment Money and Credit My Childhood Natural Vegetation and Wildlife NCERT Network connectivity devices Network Models Network Security No Men Are foreign Node.js operator overloading P5.js PHP Physical features of India Population Prime Numbers python Quick sort R language Rain on the roof Regular Expression Resources and development reversing array saakhi science Searching Algorithm Selection sort Social Media Marketing social science Software Engineering Software Testing Sorting Algorithm Stacks staircase pattern System Concepts Text Recognition The last Leaf time converter Time Passed From A Date Todo List App Tree Trending Technologies Understanding Economic Development username and password video player Visualization water resources Wired And Wireless LAN साखी
Show more

Popular Posts

Big Data MCQs(multiple choice questions) with answers - letsbug

Digital Marketing MCQ(Multiple Choice Questions) with Answers | part 1 | letsbug

Software Engineering MCQs questions with answers - letsbug