Programming/R: Difference between revisions

From Dev Wiki
Jump to navigation Jump to search
(Expand vector section)
(Correct page formatting)
Line 52: Line 52:
Unlike most other programming languages, indexes in R start at 1.
Unlike most other programming languages, indexes in R start at 1.
  my_vector <- c(5, 7, 2)
  my_vector <- c(5, 7, 2)
 
&nbsp;
  # Print first index.
  # Print first index.
  my_vector[1]
  my_vector[1]
 
&nbsp;
  # Print last index.
  # Print last index.
  my_vector[3]
  my_vector[3]
Line 61: Line 61:
Furthermore, unlike most languages, you can select multiple values at once by using a nested Vector syntax:
Furthermore, unlike most languages, you can select multiple values at once by using a nested Vector syntax:
  my_vector <- c(5, 7, 2)
  my_vector <- c(5, 7, 2)
 
&nbsp;
  # Print out first and last index at the same time.
  # Print out first and last index at the same time.
  my_vector[c(1, 3)]
  my_vector[c(1, 3)]
 
&nbsp;
  # Alternatively, to select a range of values, we can use this syntax.
  # Alternatively, to select a range of values, we can use this syntax.
  # In this case, we print out the first two indexes.
  # In this case, we print out the first two indexes.
Line 77: Line 77:
  my_vector <- c(1, 2, 3)
  my_vector <- c(1, 2, 3)
  ones_vector <- c(1, 1, 1)
  ones_vector <- c(1, 1, 1)
 
&nbsp;
  # This should create a new vector of (2, 3, 4).
  # This should create a new vector of (2, 3, 4).
  new_vector <- my_vector + ones_vector
  new_vector <- my_vector + ones_vector
Line 83: Line 83:
Alternatively, if you want to combine all values in a vector, use the {{ ic | sum() }} function.
Alternatively, if you want to combine all values in a vector, use the {{ ic | sum() }} function.
  my_vector <- c(1, 2, 3)
  my_vector <- c(1, 2, 3)
 
&nbsp;
  # This will output "6".
  # This will output "6".
  sum(my_vector)
  sum(my_vector)
Line 89: Line 89:
To get an average of array values, we can use the {{ ic | mean() }} function.
To get an average of array values, we can use the {{ ic | mean() }} function.
  my_vector <- c(1, 2, 3)
  my_vector <- c(1, 2, 3)
 
&nbsp;
  # This will output "2".
  # This will output "2".
  mean(my_vector)
  mean(my_vector)
Line 95: Line 95:
We can also test equality on every value within a Vector.
We can also test equality on every value within a Vector.
  my_vector <- c(1, 2, 3)
  my_vector <- c(1, 2, 3)
 
&nbsp;
  # Check which values are greater than 1.
  # Check which values are greater than 1.
  my_vector > 1
  my_vector > 1

Revision as of 00:13, 30 May 2020

R is a language used for statistics.


Comments

# This is an inline comment.


Variables

Variables R are loosely typed in R. That means that the type (bool, int, string, etc) is implicitly declared by the value provided.

ToDo: Link to variable typing.

Variable Definition

a_bool <- TRUE
b_bool <- FALSE
my_var_1 <- "This is "
my_var_2 <- "a string."

Variable Usage

# We can print our variables by retyping the variable name with no further syntax.
a_bool
b_bool
my_var_1
my_var_2

Variable Types

Variable types in R are called the following:

  • Booleans are called Logicals .
  • Text is called characters .
  • Numbers are called numerics .

If ever unsure you can check the typing of a variable with class() . For example:

# This will print out the typing for "my_variable".
class(my_variable)


Basic Data Structures

Vectors

In R, "Vectors" are what most other languages call "Arrays".

Arrays (vectors) in R are similar to Arrays (lists) in Python. That is, the size and semantics of the array are taken care of for you, and all you need to worry about are the values you place into it.

For the rest of this section, R arrays will be referred to by the proper name, aka Vectors.

Declaring Vectors

# Vectors in R can have mixed value types.
character_vector <- c("This", "is", "a", "character", "vector")
numeric_vector <- c(1, 2, 15, 6)
logical_vector <- c(TRUE, FALSE, FALSE)
mixed_vector <- c(TRUE, 1, "test")

Accessing Vector Values

Unlike most other programming languages, indexes in R start at 1.

my_vector <- c(5, 7, 2)
 
# Print first index.
my_vector[1]
 
# Print last index.
my_vector[3]

Furthermore, unlike most languages, you can select multiple values at once by using a nested Vector syntax:

my_vector <- c(5, 7, 2)
 
# Print out first and last index at the same time.
my_vector[c(1, 3)]
 
# Alternatively, to select a range of values, we can use this syntax.
# In this case, we print out the first two indexes.
my_vector[1:2]


Manipulating Vectors

Mathematical functions on Vectors handle very similarly to how you would expect real Mathematical Vectors to handle. Template:ToDo

For example, adding two vectors will combine the corresponding index values.

my_vector <- c(1, 2, 3)
ones_vector <- c(1, 1, 1)
 
# This should create a new vector of (2, 3, 4).
new_vector <- my_vector + ones_vector

Alternatively, if you want to combine all values in a vector, use the sum() function.

my_vector <- c(1, 2, 3)
 
# This will output "6".
sum(my_vector)

To get an average of array values, we can use the mean() function.

my_vector <- c(1, 2, 3)
 
# This will output "2".
mean(my_vector)

We can also test equality on every value within a Vector.

my_vector <- c(1, 2, 3)
 
# Check which values are greater than 1.
my_vector > 1

Dictionaries

Dictionaries in R appear to actually be modified vectors. Basically, first create your desired vector (to hold the "values"), then use the names function on it to declare keys.

Here's an example for a hypothetical business trying to track count of items sold.

product_sold <- c(50, 56, 102)
names(product_sold) <- c("Ice Cream", "Burgers", "Pizza")

Alternatively, you can create two arrays and combine them.

product_sold <- c(50, 56, 102)
product_names <- c("Ice Cream", "Burgers", "Pizza")
names(product_sold) <- product_names

The two above code snippets should be equivalent.

Once we have names (aka keys) associated with our values, we can use those to get specific indexes.

# Print out the count of pizza sold.
product_sold["Pizza"]