Vector selection by name
R Programming Language Vector in R Language (Article) Vector in R Language (Program)
251Program:
# Poker and roulette winnings from Monday to Friday: poker_vector <- c(140, -50, 20, -120, 240) roulette_vector <- c(-24, -50, 100, -350, 10) days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") names(poker_vector) <- days_vector names(roulette_vector) <- days_vector # Select poker results for Monday, Tuesday and Wednesday poker_start <- poker_vector[c("Monday","Tuesday", "Wednesday")] # Calculate the average of the elements in poker_start mean(poker_start)
Output:
> # Poker and roulette winnings from Monday to Friday: > poker_vector <- c(140, -50, 20, -120, 240) > roulette_vector <- c(-24, -50, 100, -350, 10) > days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") > names(poker_vector) <- days_vector > names(roulette_vector) <- days_vector > > # Select poker results for Monday, Tuesday and Wednesday > poker_start <- poker_vector[c("Monday","Tuesday", "Wednesday")] > > # Calculate the average of the elements in poker_start > mean(poker_start) [1] 36.66667
Explanation:
# Select poker results for Monday, Tuesday and Wednesday
poker_start <- poker_vector[c("Monday","Tuesday", "Wednesday")]
# Calculate the average of the elements in poker_start
mean(poker_start)
This Particular section is dedicated to Programs only. If you want learn more about R Programming Language. Then you can visit below links to get more depth on this subject.