Programming/R: Difference between revisions

From Dev Wiki
Jump to navigation Jump to search
(Expand page)
(Add start of vector section)
Line 4: Line 4:
== Comments ==
== Comments ==
  # This is an inline comment.
  # This is an inline comment.


== Variables ==
== Variables ==
Line 31: Line 32:
  # This will print out the typing for "my_variable".
  # This will print out the typing for "my_variable".
  class(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")

Revision as of 23:19, 29 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")