R programming solutions!

R programming language and software environment are free and it's used for statistical computing and graphics. The R language is a widely used language among statisticians and data miners for developing statistical software and data analysis. This free software that is used by data scientists was created by Ross Ihaka and Robert Gentleman in 1993. R software compiles and runs on a wide variety of UNIX platforms, Windows and macOS. Here in this article what we would like to discuss is that with practical examples for the beginners from the very first step on how to program in R and how to use R for effective data analysis.

Let's begin with how to code in R. We are assuming that you have already installed the R program/software on your device. If not you can check on: Getting Started with R and Basics.

1. Create a string 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 times the letter m repeated in the string?

a <- "Welcome"

b <- "to"

c <- "the"

d <- "R"

e <- "programming"

f <- "tutorial"

print (paste (a,b,c,d,e,f ))


number <- nchar (("Welcome to the R programming tutorial"))



uppercase <- toupper(("Welcome to the R programming tutorial"))



result <- substring("Welcome to the R programming tutorial",16,28 )


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.


vector <- c(5,NULL, 10, 15, NA)






print(sort(vector, decreasing =TRUE))

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.


# create the vectors

Age <- c(22,25)

Name <-c("james","Mathew")

Gender <-c("M", "M")

#create the data frame

input_data <-data.frame(Age, Name, Gender)


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.


X <- list( c(2,7,8), c(“A”, “B”, “C”))


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?


a <- list(5,10,15)





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.


Newlist <- list(a=1:10, b="Good morning", c="Hi")

Newlist$a = Newlist$a +1


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.


x <- list(c(1:10), "Hello", list(“a”, “b”, “c”)) unlist(x) print(x) length(x$r)

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.


x <- sample(1:10,3,replace = TRUE)

y <- sample(1:10,3,replace = TRUE)

z <- sample(1:10,3,replace = TRUE)

A <- cbind(x,y,z)

rownames(A) <- c("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.


b <- sample(1:50,12,replace = FALSE)

B <- matrix(b, nrow = 4, ncol = 3)

rownames(B) <- c("a","b","c","d")

colnames(B) <- c("x","y","z")

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)



A=matrix(v, nrow=3, ncol=4)


A<- data.frame(A)

#It can be multiplied by the following expression

as.matrix(A) %*% as.matrix A

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.


array1 <- array (1:5,c(2,3,4))

array1<- array(data = 1:24,

dim = c(2,3,4))

dimnames(array1)[[1]] <- c("a","b")

dimnames(array1)[[2]] <- c("x","y","z")

dimnames(array1)[[3]] <- c("m","n","o","p")