0% found this document useful (0 votes)
86 views11 pages

Write A R Program To Create A Vector of A Specified Type and Length

The document provides sample R code solutions for various operations on vectors, including: creating vectors of different types and lengths; adding, multiplying, dividing vectors; finding sum, mean, max, min of vectors; sorting vectors; testing for elements in vectors; and more. Each code sample is accompanied by a brief description of the vector operation it performs.

Uploaded by

Nico Mall
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views11 pages

Write A R Program To Create A Vector of A Specified Type and Length

The document provides sample R code solutions for various operations on vectors, including: creating vectors of different types and lengths; adding, multiplying, dividing vectors; finding sum, mean, max, min of vectors; sorting vectors; testing for elements in vectors; and more. Each code sample is accompanied by a brief description of the vector operation it performs.

Uploaded by

Nico Mall
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Write a R program to create a vector of a specified type and

length. Create vector of numeric, complex, logical and


character types of length 6.

Sample Solution :

R Programming Code :

x = vector("numeric", 5)

print("Numeric Type:")

print(x)

c = vector("complex", 5)

print("Complex Type:")

print(c)

l = vector("logical", 5)

print("Logical Type:")

print(l)

chr = vector("character", 5)

print("Character Type:")

print(chr)

Write a R program to add two vectors of integers type and


length 3.

Sample Solution :

R Programming Code :

x = c(10, 20, 30)

y = c(20, 10, 40)

print("Original Vectors:")

print(x)

print(y)

print("After adding two Vectors:")


z = x + y

print(z)

Write a R program to append value to a given empty vector.

Sample Solution :

R Programming Code :

vector = c()

values = c(0,1,2,3,4,5,6,7,8,9)

for (i in 1:length(values))

vector[i] <- values[i]

print(vector)

Write a R program to multiply two vectors of integers type and


length 3.

Sample Solution :

R Programming Code :

x = c(10, 20, 30)

y = c(20, 10, 40)

print("Original Vectors:")

print(x)

print(y)

print("Product of two Vectors:")

z = x * y

print(z)

Write a R program to divide two vectors of integers type and


length 3.

Sample Solution :

R Programming Code :
x = c(10, 20, 30)

y = c(20, 10, 40)

print("Original Vectors:")

print(x)

print(y)

print("After dividing Vectors:")

z = x / y

print(z)

Write a R program to find Sum, Mean and Product of a Vector.

Sample Solution :

R Programming Code :

x = c(10, 20, 30)

print("Sum:")

print(sum(x))

print("Mean:")

print(mean(x))

print("Product:")

print(prod(x))

Write a R program to find the minimum and the maximum of a


Vector.

Sample Solution :

R Programming Code :

x = c(10, 20, 30, 25, 9, 26)

print("Original Vectors:")

print(x)

print("Maximum value of the above Vector:")


print(max(x))

print("Minimum value of the above Vector:")

print(min(x))

Write a R program to sort a Vector in ascending and


descending order.

Sample Solution :

R Programming Code :

x = c(10, 20, 30, 25, 9, 26)

print("Original Vectors:")

print(x)

print("Sort in ascending order:")

print(sort(x))

print("Sort in descending order:")

print(sort(x, decreasing=TRUE))

Write a R program to test whether a given vector contains a


specified element.

Sample Solution :

R Programming Code :

x = c(10, 20, 30, 25, 9, 26)

print("Original Vectors:")

print(x)

print("Test whether above vector contains 25:")

print(is.element(25, x))

print("Test whether above vector contains 56:")

print(is.element(56, x))

Write a R program to count the specific value in each vector.


Sample Solution :

R Programming Code :

x = c(10, 20, 30, 20, 20, 25, 9, 26)

print("Original Vectors:")

print(x)

print("Count specific value(20) in above vector:")

print(sum(x==20))

Write a R program to access the last value in a given vector.

Sample Solution :

R Programming Code :

x = c(10, 20, 30, 20, 20, 25, 9, 26)

print("Original Vectors:")

print(x)

print("Access the last value of the said vector:")

print(tail(x, n=1))

Write a R program to find second highest value in a given


vector.

Sample Solution :

R Programming Code :

x = c(10, 20, 30, 20, 20, 25, 9, 26)

print("Original Vectors:")

print(x)

print("Find second highest value in a given vector:")

l = length(x)

print(sort(x, partial = l-1)[l-1])

Write a R program to find nth highest value in a given vector.


Sample Solution :

R Programming Code :

x = c(10, 20, 30, 20, 20, 25, 9, 26)

print("Original Vectors:")

print(x)

print("nth highest value in a given vector:")

print("n = 1")

n = 1

print(sort(x, TRUE)[n])

print("n = 2")

n = 2

print(sort(x, TRUE)[n])

print("n = 3")

n = 3

print(sort(x, TRUE)[n])

print("n = 4")

n = 4

print(sort(x, TRUE)[n])

Write a R program to find common elements from multiple


vectors.

Sample Solution :

R Programming Code :

x = c(10, 20, 30, 20, 20, 25, 29, 26)

y = c(10, 50, 30, 20, 20, 35, 19, 56)

z = c(10, 40, 30, 20, 20, 25, 49, 26)

print("Original Vectors:")
print("x: ")

print(x)

print("y: ")

print(y)

print("z: ")

print(z)

print("Common elements from above vectors:")

result = intersect(intersect(x,y),z)

print(result)

Write a R program to convert given dataframe column(s) to a


vector.

Sample Solution :

R Programming Code :

dfc1 = c(1, 2, 3, 4, 5)

dfc2 = c(6, 7, 8, 9, 10)

dfc3 = c(11, 12, 13, 14, 15)

dfc4 = c(16, 17, 18, 19, 20)

v <- data.frame(dfc1=1:5, dfc2=6:10, dfc3=11:15, dfc4=16:20)

print(v)

Write a R program to extract every nth element of a given


vector.

Sample Solution :

R Programming Code :

v <- 1:100

print("Original vector:")

print(v)
print("After extracting every 5th element of the said vector:")

n <- v[seq(1, length(v), 5)]

print(n)

Write a R program to list the distinct values in a vector from a


given vector.

Sample Solution :

R Programming Code :

v = c(10, 10, 10, 20, 30, 40, 40, 40, 50)

print("Original vector:")

print(v)

print("Distinct values of the said vector:")

print(unique(v))

Write a R program to find the elements of a given vector that


are not in another given vector.

Sample Solution :

R Programming Code :

a = c(0, 10, 10, 10, 20, 30, 40, 40, 40, 50, 60)

b = c(10, 10, 20, 30, 40, 40, 50)

print("Original vector-1:")

print(a)

print("Original vector-2:")

print(b)

print("Elements of a that are not in b:")

result = setdiff(a, b)

print(result)

Write a R program to reverse the order of given vector.


Sample Solution :

R Programming Code :

v = c(0, 10, 10, 10, 20, 30, 40, 40, 40, 50, 60)

print("Original vector-1:")

print(v)

rv = rev(v)

print("The said vector in reverse order:")

print(rv)

Write a R program to concatenate a vector.

Sample Solution :

R Programming Code :

a = c("Python","NumPy", "Pandas")

print(a)

x = paste(a, collapse = "")

print("Concatenation of the said string:")

print(x)

Write a R program to convert two columns of a data frame to a


named vector.

Sample Solution :

R Programming Code :

df = data.frame(code = c("R","G","W","B"),

name = c("Red", "Green", "White", "Black")

print("Original vector:")

print(df)

result = setNames(as.character(df$na
Write a R program to combines two given vectors by columns,
rows.

Sample Solution :

R Programming Code :

v1 = c(1,3,5,7,9)

v2 = c(2,4,6,8,10)

print("Original vectors:")

print(v1)

print(v2)

print("Combines the said two vectors by columns:")

result = cbind(v1,v2)

print(result)

print("Combines the said two vectors by rows:")

result = rbind(v1,v2)

print(result)

Write a R program to add 3 to each element in a given vector.


Print the original and new vector.

Sample Solution :

R Programming Code :

v = c(1, 2, NULL, 3, 4, NULL)

print("Original vector:")

print(v)

new_v = (v+3)[(!is.na(v)) & v > 0]

print("New vector:")

print(new_v)

Copy
Sample Output:

You might also like