To modify an existing variable, one can use the edit()
function edit()
edit(x)
Warning – Using CLI tools
The edit function will open the editor that is defined in the options. While in RStudio, this is a specially designed pop-up window with buttons to save and cancel, in the command line interface (CLI) this might be vi
. The heydays of this fantastic editor are over and you might never have seen it before. It is not really possible to use vi without reading the manual (e.g. via the man vi
command on the OS CLI or an online tutorial). To get out of vi
, type: [ESC]:q![ENTER]
. Note that we show the name of a key within square brackets and that all the other strings are just one keystroke each.
R is an interpreted language and while the usual interaction is typing commands and getting the reply appear on the screen, it is also possible to use R in batch mode.
batch mode
functions
1 create a file test.R
2 add the content print(“Hello World”)
3 run the command line Rscript test.R
4 now, open R and run the command source(“test.R”)
source()
1 add in the file
my_function <- function(a,b) { a +b }
1 now repeat step 4 and run my_function(4,5)
In this section we will present you with a practical introduction to R, it is not a formal introduction. If you would like to learn more about the foundations, then we recommend the documentation provided by the R-core team here: https://cran.r-project.org/doc/manuals/r-release/R-lang.pdf
.
As any computer language, R allows to use variables to store information. The variable is referred to by its name. Valid names in R consist of numbers and characters. Evenmost special characters can be used.
variables
In R, variables
can contain letters as well as “_” (underscore) and “.” (dot), and
variables must start with a letter (that can be preceded with a dot).
For example, my_var.1
and my.Cvar
are valid variables, but _myVar
, my%var
and 1.var
are not acceptable.
Assignment can be made left or right:
assignment
x.1 <-5 x.1 +3 ->.x print(.x) ## [1] 8
R-programmers will use the arrow sign <-
most often, however, R allows left assignment with the =
sign.
x.3 =3.14 x.3 ## [1] 3.14
There are also occasions that we must use the =
operator. For example, when assigning values to named inputs for functions.
v1 <- c(1,2,3,NA) mean(v1, na.rm = TRUE) ## [1] 2
There are more nuances and therefore, we will come back to the assignment in Chapter 4.4.4 “ Assignment Operators ” on page 78. These nuances are better understood with some more background, and for now it is enough to be able to assign values to variable.
With what we have seen so far, it is possible to make already simple calculations, define and modify variables. There is still a lot to follow and it is important to have some basic tools to keep things tidy. One of such tools is the possibility to see defined variables and eventually remove unused ones.
# List all variables ls() # hidden variable starts with dot ls(all.names = TRUE) # shows all # Remove a variable rm(x.1) # removes the variable x.1 ls() # x.1 is not there any more rm(list = ls()) # removes all variables ls()
Note – What are invisible variables
A variable whose name starts with a dot (e.g. .x
) is in all aspects the same as a variable that starts with a letter. The only difference is that the first will be hidden with the standard arguments of the function ls()
.
ls()
rm()
As most computer languages, R has some built-in data-types. While it is possible to do certain things in R without worrying about data types, understanding and consciously using these base-types will help you to write bug-free code that is more robust and it will certainly speed up the debugging process. In this section we will highlight the most important ones.
4.3.1 The Elementary Types
There is no need to declare variables explicitly and tell R what type the variable will be before using it. R will assign them a class whenever this is needed and even change the type when our code implies a change.
class()
long
complex numbers
string
# Booleans can be TRUE or FALSE:x <-TRUE class(x) ## [1] "logical" # Integers use the letter L (for Long integer):x <-5L class(x) ## [1] "integer" # Decimal numbers, are referred to as ‘numeric’:x <-5.135 class(x) ## [1] "numeric" # Complex numbers use the letter i (without multiplication sign):x <-2.2 +3.2i class(x) ## [1] "complex" # Strings are called ‘character’:x <-"test" class(x) ## [1] "character"
Warning – Changing data types
While R allows to change the type of a variable, doing so is not a good practice. It makes code difficult to read and understand.
# Avoid this:x <-3L # x defined as integerx ## [1] 3 x <-"test" # R changes data typex ## [1] "test"
So, keep your code tidy and to not change data types.
Working with dates is a complex subject. We explain the essence of the issues in Section 17.6 “ Dates with lubridate ” on page 407. For now, it is sufficient to know that dates are one of the base types of R.
date
# The function as.Data coerces its argument to a dated <- as.Date( c("1852-05-12", "1914-11-5", "2015-05-01")) # Dates will work as expectedd_recent <- subset(d, d > as.Date("2005-01-01")) print(d_recent) ## [1] "2015-05-01"
as.Date()
subset()
Further information –More about dates
Make sure to read Section 17.6 “ Dates with lubridate ” on page 407 for more information about working with dates as well as the inevitable problems related to dates.
Читать дальше