Assignments and Basic Operations in R

From AWF-Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "==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" (<-) o...")
 
(Blanked the page)
 
Line 1: Line 1:
==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] 2
 
 
b
 
 
## [1] 3
 
 
c
 
 
## [1] 4
 
 
Operating with objects in '''R''' is very easy. Below some examples with single values:
 
 
(a+b)/c*5
 
 
##[1] 6.25
 
 
and with vectors.
 
 
v.1 <- v*exp((a+b)/c*5)
 
v.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
 
 
#  A b C E
 
# 1 1 4 low 7
 
# 2 2 5 medium 8
 
# 3 3 6 high 9
 
 
Logical tests can also be done in '''R'''.
 
 
e <- c<a
 
e
 
 
## [1] FALSE
 

Latest revision as of 17:10, 31 January 2015

Personal tools
Namespaces

Variants
Actions
Navigation
Development
Toolbox
Print/export