Assignments and basic operations in R
An assignment is the fact of storing an object under a given name. Assignments in \(\textsf{R}\) are done by using the "arrow" ({\tt <-}) or the equal ({\tt =}) 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 b c @ Operating with objects in \textsf{R} is very easy. Below some examples with single values: <<>>= (a+b)/c*5 @
and with vectors. <<>>= v.1 <- v*exp((a+b)/c*5) v.1 @ 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 {\tt 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 {\tt d} are replaced by the characters {\tt "low"}, {\tt "medium"} and {\tt "high"} as follows: <<>>= d$C <- c("low", "medium", "high") d @ The assignments can also be to a previously non-existent column in the dataframe. <<>>= d$E <- m[,3] d @
Logical tests can also be done in \testsf{R}. <<>>= e <- c<a e @