0% found this document useful (0 votes)
2 views

DA Experiment - 2

This document introduces vectors in R programming, explaining their role as a fundamental data structure for storing sequences of elements of the same type. It covers how to create vectors using the c() function, indexing, vectorized operations, and various built-in functions for manipulation. Additionally, it provides a series of programming exercises related to arithmetic operations, distinct values, reversing order, sorting, and statistical calculations on vectors.

Uploaded by

numanhussain141
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DA Experiment - 2

This document introduces vectors in R programming, explaining their role as a fundamental data structure for storing sequences of elements of the same type. It covers how to create vectors using the c() function, indexing, vectorized operations, and various built-in functions for manipulation. Additionally, it provides a series of programming exercises related to arithmetic operations, distinct values, reversing order, sorting, and statistical calculations on vectors.

Uploaded by

numanhussain141
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical No.

– 2
Objective - Introduction to vector in R programming.

In R programming, a vector is a fundamental data structure used to store a


sequence of elements of the same data type, such as numeric, character, or logical
values. Vectors are created using the c() function, which stands for combine,
allowing you to concatenate elements into a single vector. For example,
numbers <- c(1, 2, 3, 4, 5) creates a numeric vector. R automatically converts
elements to a common type if mixed types are combined. For instance, mixing
numeric and character elements will result in all elements being converted to
characters. This strict data type consistency is essential for efficient data
manipulation and computation. Vectors are widely used in R for calculations,
statistical analysis, and data manipulation, serving as building blocks for more
complex data structures like matrices, data frames, and lists.

Vectors in R are indexed starting from 1, unlike many other programming


languages that use zero-based indexing. This means the first element of a vector
is accessed using the index 1, such as numbers[1], which would return 1 in the
previous example. R supports vectorized operations, allowing arithmetic or
logical operations to be applied to each element automatically. For example,
adding two vectors of the same length adds their corresponding elements.
Subsetting vectors is done using square brackets, like numbers[2:4] to extract the
second to fourth elements. R provides various built-in functions for vector
manipulation, including length() to find the number of elements, sum() to
calculate the total of numeric elements, and sort() to arrange them in ascending
or descending order. These features make vectors powerful and versatile tools for
data analysis in R.
Q.1 – Write a program to perform various arithemetic operations and some
miscellaneous operations on two vectors provided by the user for length 5.

Input -

Output –

Q.2 – Write a program to identify the distinct values from a given vector.

Q.3 – Write a program to reverse order of a given vector.

Q.4 – Write a program to arrange the values of a vector in ascending and


descending order.

Q.5 – Write a program to find the second last value from the given vector.
Q.6 – Write a program to perform different operations like max, min ,average,
square root , variance and standard deviation.

Q.7 – Write a program to find a common elements among three vectors.

Q.8 – Write a program to count the number of values in a specific range from a
given vector.

You might also like