Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

TASK 2

1. Create a strings with the name “Welcome to the R programming tutorial” and do the
following.
a) How many characters are there including the space character?
b) Change the whole strings into uppercase.
c) Extract the “R programming” from the string and print it.
d) Convert all letters ‘a’ and ‘i’ in a string into ‘A’ and ‘I’.
e) How many time the letter m repeated in the string?
2. Create a vector of length 5 that includes the numerical value with NULL and NA value.
Eg: x <- c(10, NULL, 20, 30, NA)
And do the following.
a) Write an R program to find Sum, Mean and Product of a Vector, ignore element like
NA or NaN.
b) Sort the vector with descending order.
c) Extract the value “NULL” and “NA” from the vector.

3. If:
Age <- c(22, 25, 18, 20)
Name <- c(“james”, “Mathew”, “Olivia”, “stella”)
Gender <- c(“M”, “M”, “F”, “F”)

Then what is the R-code for getting the following output.


Age Name Gender
22 james M
25 mathew M

4. IF
W <- c(2,7,8)
V <- c(“A”, “B”, “C”)

a) Create a list containing W and V and name it as X.


b) Replace the value “A” in X with “K”.

5. If a <- list (5, 10, 15). How can we do to get the sum of all elements in a?
6. If Newlist <- list(a=1:10, b="Good morning", c="Hi"), write an R statement that will add
1 to each element of the first vector in Newlist.
7. If x <- list(c(1:10), "Hello", list(“a”, “b”, “c”)). write an R statement that will assign new
names "vector", "string" and "list" to the elements of x. and also write a R statement that
will give the length of vector of x.
8. Create three vectors x,y,z with integers and each vector has 3 elements. Combine the
three vectors to become a 3×3 matrix A where each column represents a vector. Change
the row names to a,b,c.
9. Create a vector with 12 integers. Convert the vector to a 4*3 matrix B using matrix().
Please change the column names to x, y, z and row names to a, b, c, d.
The argument byrow in matrix () is set to be FALSE by default. Please change it to
TRUE and print B to see the differences.
10. If we convert A to a data.frame type instead of a matrix , can we still compute a
conventional matrix multiplication for matrix A multiplies matrix A ? Is there any way
we could still perform the matrix multiplication for two data.frame type variables?
(Assuming proper dimension)
11. Create an array (3 dimensional) of 24 elements. Assign some dimnames of your choice to
the array. Do the following
a) Print the matrix of third dimension.
b) Print all row of second column of second dimension.
c) Print element of first row, second column and third dimension.
12. Create the following data frame with sex as a factor
Name Age Height Weight Sex
Alex 25 177 57 F
Lilly 31 163 69 F
Mark 23 190 83 M
Oliver 52 179 75 M
Martha 76 163 70 F
Lucas 49 183 83 M
Caroline 26 164 53 F

a) Create this dataframe and make sure that variable Working as character and not
factor.
Name Working
Alex Yes
Lilly No
Mark No
Oliver Yes
Martha Yes
Lucas No
Caroline Yes

Add this data frame column-wise to the previous one.


i) How many rows and columns does the new data frame have?
ii) What class of data is in each column?

b) Add the row with [age = 34, Height = 129, Weight = 89, Sex = F, Working = Yes].
13. Install any one R- packages in your R environment and load the package to your library.

You might also like