R

¡Supera tus tareas y exámenes ahora con Quizwiz!

vector

Vectors are one-dimension arrays that can hold numeric data, character data, or logical data. Stated otherwise, a vector is a simple tool to store data.

sum().

sum(). It calculates the sum of all elements of a vector. For example, to calculate the total amount of money you've lost/won with poker you do: total_poker = sum(poker_vector).

Basic data types in R

R works with numerous data types. Some of the most basic types to get started are: Decimals values like 4.5 are called numerics. Natural numbers like 4 are called integers. Boolean values (TRUE or FALSE) are called logical (TRUE can be abbreviated to T and FALSE to F). Text (or string) values are called characters. Note how the quotation marks on the right indicate that "some text" is a character.

What's that data type?

Remember when you added 5 + "six" and got an error due to a mismatch in data types? You avoid such embarrassing situations by checking the data type of a variable upfront. You can do this as follow: class(some_variable_name).

Vector selection: 3

Selecting multiple elements of poker_vector with c(2,3,4) is not very convenient. Many statisticians are lazy people by nature, so they created an easier way to do this: c(2,3,4) can be abbreviated to2:4, which generates a vector with all natural numbers from 2 up to 4. Another way to find the mid-week results is thus poker_vector[2:4]. Notice how the vector 2:4 is placed between the square brackets to select element 2 up to 4.

R has five basic atomic classes of objects.

So these are the kind of a very low level, or, or basic classes of objects. And they are character numeric so these are like real numbers or decimal numbers. integers, complex numbers and logicals. So logicals are just true false type things.

Vector selection: 2

To select multiple elements from a vector, you can add square brackets at the end of it. Between the square brackets, you should indicate what element should be selected. For example: suppose you want to select the first and the fifth day of the week: use the vector c(1,5) between the square brackets: poker_vector[c(1,5)]. Thus, you select here the first and the fifth element of poker_vector.

assign the days of the week vector to a variable

# Create the variable days_vector containing "Monday","Tuesday","Wednesday","Thursday","Friday" days_vector = c("Monday","Tuesday","Wednesday","Thursday","Friday")

variable

A basic concept in (statistical) programming is called a variable

arithmetic operators

Addition: + Subtraction: - Multiplication: * Division: / Exponentiation: ^ Modulo: %%

Vector selection 4

Another way to tackle the previous exercise, is by using the names of the vector elements (Monday, Tuesday, ...) instead of their numeric positions. For example, poker_vector["Monday"] will select the first element of poker_vector since "Monday" is the name of that first element. Just as you did in the previous exercise with numerics, you can also use the element names to select multiple elements, for example: poker_vector[c("Monday","Tuesday")].

In R, you create a vector

In R, you create a vector with the combine function c(). Between the brackets you place the vector elements separated by a comma. For example: numeric_vector = c(1,2,3) character_vector = c("a","b","c") boolean_vector = c(TRUE,FALSE) Once you have created these vectors in R, you can use them to do calculations.

Vector selection

In other words, our goal is to select specific elements of the vector. To select elements of a vector (and later matrices, data frames, ...) you can use square brackets. Between the square brackets, you indicate what elements to select: [...elements to select..]. For example, to select the first element of the vector, you type poker_vector[1]. To select the second element of the vector, you type poker_vector[2], etc.

The (logical) comparison operators known to R are:

The (logical) comparison operators known to R are: < for less than > for greater than >= for greater than or equal to == for equal to each other != not equal to each other As seen in the previous chapter, stating 6 > 5 returns TRUE. The nice thing about R is that you can use these comparison operators also on vectors. For example, the statement c(4,5,6) > 5 returns: FALSE FALSE TRUE. In other words, you test for every element of the vector if the condition stated by the comparison operator is TRUE or FALSE. Don't take our word for it, try it in the console ;-).

The ^ operator

The ^ operator takes the number left to it to the power of the number on its right-hand side: for example 3^2 is 9.

The modulo returns

The modulo returns the remainder of the division of the number left to it by the number on its right-hand side, for example 5 modulo 3 or 5%%3 is 2.

Naming a vector

You can give a name to the elements of a vector with the names() function. For example: names(some_vector). names(some_vector) = c ("Monday", "Tuesday", "Wednesday", "Thursday","Friday"


Conjuntos de estudio relacionados

Chapter 14 - The Brain and Cranial Nerves

View Set

Pearson Vue Life Insurance Practice Exam Update

View Set

Chapter 18 Mutations and DNA Repair

View Set

Unit 1 - Regulation of Investment Advisers

View Set

human nutri study guide for final

View Set