Vector Operation Tushar shinde
**creation of vector
Tushar shinde
There are number of ways by which we can create a vector
1]using c() function-
In R, we use c() function to create a vecdtor.
this function returns a one-dimensional array or simply vector.
The c() function is a generic function which combines its argument.
- all arguments are restricte with a common data type which is the type of
the returned value.
There are various other ways to creat a vector in R,
which are as follows :
we can uussee the c function to combine the values as a vector.
# by default the type will be double
EX- v1 <- c(1,2,3,4)
cat('using c function', v1,'\n')
output:
using c funtion 1 2 3 4
2]Using the ':' operator:
We can create a vector with the help of the colon operator
to create vector of continuous values
v2 <- 1:5
cat('using colon',v2)
3] using the seq() function -
In R, we can create a vector with the help of the seq() function.
A sequence function creates a sequence of elements as a vector.
There are also two ways in this.
The first way is to set the step size and the second method is by
setting the length of the vector.
seq_v <- seq(1,4,length.out =6)
cat('using seq() function', seq_v, '\n')
output:
using seq() function 1 1.6 2.2 2.8 3.4 4
Seq_v2 <- seq(1,4,by = 0.5)
cat('using seq() function by', Seq_v3, '\n')
output:
using seq() function by 1 1.5 2.0 2.5 3.0 4
How th Access Elements of R Vectors?
> a <- c(1,2,3,4)
> a
[1] 1 2 3 4
> b<- c(1,2,3,6)
> b
[1] 1 2 3 6
Vector Addition:
> a+b
[1] 2 4 6 10
> v2 <- 1:5
> v2
[1] 1 2 3 4 5
2. Using cat() Function
> cat('using colon',v2)
using colon 1 2 3 4 5
>
> v1 <- c(1,2,3,4)
> cat('using c function', v1,'\n')
using c function 1 2 3 4
>
3. Using seq() Function
> seq_v <- seq(1,4,length.out =6)
> cat('using seq() function', seq_v, '\n')
using seq() function 1 1.6 2.2 2.8 3.4 4
>
> Seq_v2 <- seq(1,4,by = 0.5)
> cat('using seq() function by', Seq_v2, '\n')
using seq() function by 1 1.5 2 2.5 3 3.5 4
>
> >
> x <- 10
> abs(x) # absolute value
[1] 10
> sqrt(x) # square root
[1] 3.162278
> exp(x) # exponential transformation
[1] 22026.47
> log(x) # logarithmic transformation
[1] 2.302585
> cos(x) # cosine and other trigonometric functions
[1] -0.8390715
>
# Create a vector.
apple <- c('red','green',"yellow")
print(apple) # Get the class of the vector.
[1] "red" "green" "yellow"
print(class(apple))
[1] "character
Creating and Using Logical Index Vectors
>
> numbers <- c(10, 20, 30, 40, 50)
>
> newnum <- numbers > 35
>
> print(newnum) Tushar shinde Tushar shinde
[1] FALSE FALSE FALSE TRUE TRUE
>
> filtered <- numbers[newnum]
> print(filtered)
[1] 40 50
>
>
1. Numeric Indexing with Vectors
> numbers <- c(10, 20, 30, 40, 50)
> print(numbers[1])
[1] 10
>
> print(numbers[c(1, 3, 5)])
[1] 10 30 50
>
Handling Duplicates in Vectors
vec <- c(1, 2, 3, 2, 4, 5, 5, 6)
duplicates <- duplicated(vec)
print(duplicates)
# Output: FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE
.Using Range Indices with Vectors
# Create a numeric vector
vec <- c(10, 20, 30, 40, 50)
# Select elements from index 2 to 4
subset_vec <- vec[2:4]
print(subset_vec)
# Output: 20 30 40
Out-of-Order Indices with Vectors
vec <- c(10, 20, 30, 40, 50) Tushar shinde
print(vec[2,1,4,3,5])
#output 20 10 40 30 50
Creating Vectors
Using c() Function: The most common way to create a vector is by using the c() function, which combines elements into a vector.
R
Copy code
v <- c(1, 2, 3, 4, 5)
Using seq() Function: For creating sequences of numbers.
R
Copy code
v <- seq(1, 10, by=2) # Creates a vector: 1, 3, 5, 7, 9
Using rep() Function: To repeat elements.
R
Copy code
v <- rep(1:3, times=3) # Creates a vector: 1, 2, 3, 1, 2, 3, 1, 2, 3
3. Write a program to sorting a vector of strings in both ascending and descending order
# Define a vector of strings
strings <- c("banana", "apple", "cherry", "date")
# Sort in ascending order
ascending_order <- sort(strings)
# Sort in descending order
descending_order <- sort(strings, decreasing = TRUE)
# Print results
cat("Ascending Order:\n")
print(ascending_order)
cat("\nDescending Order:\n")
print(descending_order)
simple R program that reverses a number and calculates the sum of its digits.
reverse_sum <- function(n)
{
sum_digits <- 0
rev <- 0
while (n > 0)
{
r <- n %% 10 # Get the last digit
sum_digits <- sum_digits + r # Add digit to sum
rev <- rev * 10 + r # Append digit to reversed number
n <- n %/% 10 # Remove the last digit
}
print(paste("Sum of digits:", sum_digits))
print(paste("Reverse of number:", rev))
}
# Read input from user
n <- as.integer(readline(prompt = "Enter a number: "))
# Call the function with user input
reverse_sum(n)
[1] "Sum of digits: 15"
[1] "Reverse of number: 54321"
No comments:
Post a Comment