Assignments and Basic Operations in R

From AWF-Wiki
Revision as of 14:44, 25 January 2015 by Aknop (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Assignments and basic operations in R

An assignment is the fact of storing an object under a given name. Assignments in R are done by using the "arrow" (<-) or the equal (=) symbol, even though it can be recommended to use the arrow because some functions use the equal symbol as an argument. Assignments can be done in two directions. Some examples are shown below.

a <- 2 b <- 3 4 -> c a

    1. [1] 2

b

    1. [1] 3

c

    1. [1] 4

Operating with objects in R is very easy. Below some examples with single values:

(a+b)/c*5

    1. [1] 6.25

and with vectors.

v.1 <- v*exp((a+b)/c*5) v.1

    1. [1] 518 1036 1554 2072 2590 3108 3626 4144 4662

Note that in the first case the calculus was computed and displayed in the console, but the result was not assigned to any object. In the second case, the result of the calculus was assigned to a new object named v.1. In the assignment process it must be considered that objects can be overwritten as many times as we want, and the information of the original object can be lost if the required attention is not played.

Assignments can also be done to positions in an existent object. In the following example, the values in the third column of the data frame d are replaced by the characters low, medium and high as follows:

d$C <- c("low", "medium", "high") d # A b C # 1 1 4 low # 2 2 5 medium # 3 3 6 high The assignments can also be to a previously non-existent column in the dataframe. d$E <- m[,3] d

  1. A b C E
  2. 1 1 4 low 7
  3. 2 2 5 medium 8
  4. 3 3 6 high 9

Logical tests can also be done in R.

e <- c<a e

    1. [1] FALSE
Personal tools
Namespaces

Variants
Actions
Navigation
Development
Toolbox
Print/export