How to Create, Modify, and access elements of Create, Modify, access elements of Vectors in Rstudio

# Let us create vectors in R
# Vectors are one-dimension arrays that can hold numeric data, character data, or logical data.

# vector defination in R 
# numeric vector
age <- c(3, 25, 30, 55, 60)

#character vector
abc <- c("Ram", "Rahim", "Joseph", "SIdhu", "Mahaveer")
abc
[1] "Ram" "Rahim" "Joseph" "SIdhu" "Mahaveer"

# Logical vector
logical_vector <- c(TRUE, TRUE, TRUE, FALSE, FALSE )

# How to assign names to the vector

names(age) <- c("Ram", "Rahim", "Joseph", "SIdhu", "Mahaveer")
age
Ram Rahim Joseph SIdhu Mahaveer
3 25 30 55 60


names(age) <- abc
age
Ram Rahim Joseph SIdhu Mahaveer
3 25 30 55 60

age <- c(3, 25, 30, 55, 60)
age2 <- c(6, 7, 8, 9, 10)

Total_age <- age + age2
Total_age
[1] 9 32 38 64 70

total_ag <- sum(age)
total_ag
[1] 173

total_ag2 <- sum(age2)
total_ag2
[1] 40


total_ag > total_ag2
[1] TRUE

total_ag <- mean(age)
total_ag
[1] 34.6

total_ag2 <- mean(age2)
total_ag2
[1] 8

total_ag == total_ag2
[1] FALSE

total_ag != total_ag2
[1] TRUE

# Let us go to las vegas for gambling

# Poker winnings from Monday to Friday
poker_vector <- c(140, -50, 20, -120, 240)

# Roulette winnings from Monday to Friday
roulette_vector <- c(24, 50, 100, 350, 10)

# let us add names to the winning days
names(poker_vector) <-c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
poker_vector
Monday Tuesday Wednesday Thursday Friday
140 -50 20 -120 240

names(roulette_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

## Alternatively
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

names(poker_vector) <- days_vector

names(roulette_vector)<- days_vector

## What was your net profit / loss every day

total_daily <- poker_vector + roulette_vector

total_daily
Monday Tuesday Wednesday Thursday Friday
164 0 120 230 250

 # Total winnings with poker
 total_poker <- sum(poker_vector)

 # Total winnings with roulette
 total_roulette <-sum(roulette_vector)
 total_week <- (total_poker + total_roulette)
 total_week
[1] 764

 # Check if you realized higher total gains in poker than in roulette
 total_poker > total_roulette
[1] FALSE

 # Define a new variable based on a selection
 poker_wednesday <- poker_vector[3]

# Define a new variable based on a selection
 poker_midweek <- poker_vector[c(2, 3, 4)]

poker_midweek <- poker_vector [2:4]
 poker_midweek
Tuesday Wednesday Thursday
-50 20 -120
 poker_monday <- poker_vector ["Monday"]

 poker_monday
Monday
140
 average_midweek_gain <- mean(poker_vector[c("Monday", "Tuesday", "Wednesday")])
 average_midweek_gain <- mean(poker_vector [2:4])

 # What days of the week did you make money on poker?
 selection_vector <- poker_vector > 0

 # Select from poker_vector these days
 poker_winning_days <- poker_vector[selection_vector]

 poker_winning_days
Monday Wednesday Friday
140 20 240

# What days of the week did you make money on roulette?
 selection_vector <- roulette_vector > 0

 # Select from roulette_vector these days
 roulette_winning_days <- roulette_vector[selection_vector]

Leave a Reply

Your email address will not be published. Required fields are marked *