l03 l04 FDA-lab Sheet 4
l03 l04 FDA-lab Sheet 4
Lab-4 (L03+L04)
Q1. Sort the matrix x <- matrix(1:100, ncol=10):
in descending order by its second column (call the sorted matrix x1)
in descending order by its second row (call the sorted matrix x2)
Q2. Consider the following array 'arr' and predict the outcome of each of the following print
statements.
arr=array(1:9, c(3,3,2))
print(sum(arr[,,1]))
print(sum(arr[,,1]==arr[,,2]))
print(arr[1,,1]==arr[1,,2])
print(max(arr[,1,1]))
print(arr[,1,])
Q3. Create a dataframe df with 40 columns, as follows:
df <- as.data.frame(matrix(sample(1:5, 2000, T), ncol=40))
Sort the dataframe on all 40 columns, from left to right, in increasing order.
Sort the dataframe on all 40 columns, from left to right, in decreasing order.
Sort the dataframe on all 40 columns, from right to left, in increasing order
Q4. Extract unique elements from: x <- c(9:20, 1:5, 3:7, 0:8)
Q5. What is the output of the syntax given below:
(xu <- x[!duplicated(x)])
(xu2 <- x[!duplicated(x, fromLast = TRUE)])
Q6. Create a matrix of 4 X 5 containing duplicate elements and print unique elements from it.
Q7. Create a data frame containing duplicate elements and print only the unique elements from it.
Q8. Create a vector that contain all numbers from 1 to 17, where each number occurs the same number
of times as the number itself eg. 1, 2, 2, 3, 3, 3…
Q9. What will be the result of the following calculations?
c(1, 3, 5) + c(2, 4, 6)
c(1, 3, 5) + c(2, 4, 6, 8)
c(1, 3) - c(2, 4, 6 ,8)
Try to think about your expectations prior to running the code in R.
Exercises on - Basics of R:
Q10. Write a program in R to enter any number and print all factors of the number.
Sample Output:
Input a number: 63
The factors are: 1 3 7 9 21 63
Q11. Write an R program to create an integer vector of length 10 and print the second largest element
present in it.
Q12. Write a program in R to find one's complement of a binary number.
Sample Output:
Input a 8 bit binary value: 10100101
The original binary = 10100101
After ones complement the number = 01011010
Q13. Write a program in R to find two's complement of a binary number.
Sample Output:
Input an 8-bit binary value: 01101110
The original binary = 01101110
After ones complement the value = 10010001
After twos complement the value = 10010010
Q14. Write a program in R to convert a decimal number to binary number.
Sample Output:
Input a decimal number: 35
The binary number is: 100011
Q15. Write a program in R to convert a decimal number to hexadecimal number.
Sample Output:
Input a decimal number: 43
The hexadecimal number is: 2B
Q16. Write a program in R to convert a decimal number to octal number.
Sample Output:
Input a decimal number: 15
The octal number is: 17
Q17. Write a program in R to convert a binary number to decimal number.
Sample Output:
Input a binary number: 1011
The decimal number: 11
Q18. Write a program in R to convert a binary number to hexadecimal number.
Sample Output:
Input a binary number: 1011
The hexadecimal value: B
Q19. Write a program in R to convert a binary number to octal number.
Sample Output:
Input a binary number: 1011
The equivalent octal value of 1011 is: 13
Q20. Consider the below given data frame student and write R commands to execute the following
tasks:
id=1:10
name=letters [1:10]
age=sample(18:22, 10, replace=T)
score=sample(1:50, 10, replace=T)
student=data.frame(id, name, age, score)
(i) Print the range of the score attribute.
(ii) Print the details of those students whose score is more than 30.
(iii) Print the details of those students who have received the maximum score.
(iv) Print the names of those students whose age is less than 20.
(v) Print the identities of those students whose score is more than the mean of all the score
values.