- 2148
- 2024/07/13 - 09:57
- 45 بازدید
R (programming language) training course. By:Babak Babaabasi (1)R Tutorial: Why Are There So Many Programming Languages? There are so many programming languages out there, and more are developed every few years. We have Python, JavaScript, PHP, C++, Ruby, Java, C#, and way too many more to list here. Here are some developer jobs and the main programming languages they use: Game developers use C++ or C# to make video games for PCs and consoles. Web developers use[…]
R (programming language) training course. By:Babak Babaabasi
(1)R Tutorial:
Why Are There So Many Programming Languages?
There are so many programming languages out there, and more are developed every few years. We have Python, JavaScript, PHP, C++, Ruby, Java, C#, and way too many more to list here.
Here are some developer jobs and the main programming languages they use:
- Game developers use C++ or C# to make video games for PCs and consoles.
- Web developers use HTML, CSS, JavaScript, and PHP to make websites and web applications.
- Mobile app developers use Java and Kotlin to make Android applications or use Swift to make iOS applications.
- Software developers use C++, C#, and Java to make desktop applications, business applications, and system software.
- Data scientists use Python, R, and MatLab to analyze data for scientific research and educational purposes.
top data science programming languages:
download R
install r
run r
first code with r
print(“helooooo”)
## [1] “helooooo”
#or you can type
“heloooo”
## [1] “heloooo”
first calculation with r
5+5
## [1] 10
IDE
r studio
why r stodu
Here are the major windows (or panes) of the RStudio environment:
what is the difference between r script and r notebook
The primary difference is that when executing chunks in an R Markdown document, all the code is sent to the console at once, but in a notebook, only one line at a time is sent. This allows execution to stop if a line raises an error.
r script example
r notebook example
R Syntax
To output text in R, use single or double quotes: To output numbers, just type the number (without quotes)
“heloooo”
## [1] “heloooo”
# for number without quotes
5+5
## [1] 10
R Print Output
Unlike many other programming languages, you can output code in R without using a print function:
print(“helooooo”)
## [1] “helooooo”
And there are times you must use the print() function to output code, for example when working with for loops (which you will learn more about in a later chapter):
for (x in 1:10) {
print(x)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
R Comments
Comments starts with a #. When executing code, R will ignore anything that starts with #.
Unlike other programming languages, such as Java, there are no syntax in R for multiline comments. However, we can just insert a # for each line to create multiline comments:
# This is a comment
# written in
# more than just one line
“Hello World!”
## [1] “Hello World!”
R Variables
To assign a value to a variable, use the <- or = signs🥴. To output (or print) the variable value, just type the variable name:
name = “babak”
age <- 36
surname=”babaabasi”
# output Variables
name
## [1] “babak”
age
## [1] 36
surname
## [1] “babaabasi”
Concatenate Elements
You can also concatenate, or join, two or more elements, by using the paste() function.
To combine both text and a variable, R uses comma (,):
print(c(name,surname))
## [1] “babak” “babaabasi”
print(c(name,” “,surname,” is “,age,” years old.”))
## [1] “babak” ” ” “babaabasi” ” is ” “36”
## [6] ” years old.”
print(paste(name,”_”,surname, “is” ,age,” years old.”))
## [1] “babak _ babaabasi is 36 years old.”
======
ages=c(26,23,35,56)
mean(ages)
## [1] 35
m=mean(ages)
text <- “awesome”
paste(“R is”, text)
## [1] “R is awesome”
text1 <- “R is”
text2 <- “awesome”
paste(text1, text2)
## [1] “R is awesome”
For numbers, the + character works as a mathematical operator:
num1 <- 5
num2 <- 10
num1 + num2
## [1] 15
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
Rules for R variables are:
- A variable name must start with a letter and can be a combination of letters, digits, period(.)
and underscore(_). If it starts with period(.), it cannot be followed by a digit. - A variable name cannot start with a number or underscore (_)
- Variable names are case-sensitive (age, Age and AGE are three different variables)
- Reserved words cannot be used as variables (TRUE, FALSE, NULL, if…)
# Legal variable names:
myvar <- “John”
my_var <- “John”
myVar <- “John”
MYVAR <- “John”
myvar2 <- “John”
.myvar <- “John”
R Data Types
In programming, data type is an important concept.
Variables can store data of different types, and different types can do different things.
In R, variables do not need to be declared with any particular type, and can even change type after they have been set:
my_var <- 30 # my_var is type of numeric
my_var <- “Sally” # my_var is now of type character (aka string)
Basic Data Types
Basic data types in R can be divided into the following types:
- numeric – (10.5, 55, 787)
- integer – (1L, 55L, 100L, where the letter “L” declares this as an integer)
a number which is not a fraction; a whole number.”integer values”
- complex – (9 + 3i, where “i” is the imaginary part)
- character (a.k.a. string) – (“k”, “R is exciting”, “FALSE”, “11.5”)
- logical (a.k.a. boolean) – (TRUE or FALSE)
We can use the class() function to check the data type of a variable:
# numeric
x = 10.5
class(x)
## [1] “numeric”
# integer
x2 <- 1000L
class(x)
## [1] “numeric”
# complex
x <- 9i + 3
class(x)
## [1] “complex”
# character/string
x <- “R is exciting”
class(x)
## [1] “character”
# logical/boolean
x <- TRUE
class(x)
## [1] “logical”
R Numbers
x <- 10.5 # numeric
y <- 10L # integer
z <- 1i # complex
x <- 10.5
y <- 55
# Print values of x and y
x
## [1] 10.5
y
## [1] 55
# Print the class name of x and y
class(x)
## [1] “numeric”
class(y)
## [1] “numeric”
x <- 1000L
y <- 55L
# Print values of x and y
x
## [1] 1000
y
## [1] 55
# Print the class name of x and y
class(x)
## [1] “integer”
class(y)
## [1] “integer”
x <- 3+5i
y <- 5i
# Print values of x and y
x
## [1] 3+5i
y
## [1] 0+5i
# Print the class name of x and y
class(x)
## [1] “complex”
class(y)
## [1] “complex”
Type Conversion
You can convert from one type to another with the following functions:
- as.numeric()
- as.integer()
- as.complex()
x=10.8
class(x)
## [1] “numeric”
b=as.integer(x)
class(b)
## [1] “integer”
b
## [1] 10
x <- 1L # integer
y <- 2 # numeric
# convert from integer to numeric:
a <- as.numeric(x)
# convert from numeric to integer:
b <- as.integer(y)
# print values of x and y
x
## [1] 1
y
## [1] 2
# print the class name of a and b
class(a)
## [1] “numeric”
class(b)
## [1] “integer”
g=10.5
g
## [1] 10.5
class(g)
## [1] “numeric”
h=as.integer(g)
h
## [1] 10
class(h)
## [1] “integer”
R Math
Built-in Math Functions
R also has many built-in math functions that allows you to perform mathematical tasks on numbers.
For example, the min() and max() functions can be used to find the lowest or highest number in a set:
b=c(4,55,6,87,33,45)
min(b)
## [1] 4
max(b)
## [1] 87
mean(b)
## [1] 38.33333
h=log(b)
b
## [1] 4 55 6 87 33 45
h
## [1] 1.386294 4.007333 1.791759 4.465908 3.496508 3.806662
f=c(“a”,9,3,4,”babak”,”hasan”,36)
min(f)
## [1] “3”
v=mean(f)
## Warning in mean.default(f): argument is not numeric or logical: returning NA
max(f)
## [1] “hasan”
g=as.numeric(f)
## Warning: NAs introduced by coercion
g
## [1] NA 9 3 4 NA NA 36
min(g,na.rm = T)
## [1] 3
max(g)
## [1] NA
mean(g,na.rm = T)
## [1] 13
log(g)
## [1] NA 2.197225 1.098612 1.386294 NA NA 3.583519
max(5, 10, 15)
## [1] 15
min(5, 10, 15)
## [1] 5
abs()
The abs() function returns the absolute (positive) value of a number:
abs(-4.7)
## [1] 4.7
abs(c(3,-4,-45))
## [1] 3 4 45
k=c(-4,-5.5)
f=abs(k)
f
## [1] 4.0 5.5
ceiling() and floor()
The ceiling() function rounds a number upwards to its nearest integer, and the floor() function rounds a number downwards to its nearest integer, and returns the result:
ceiling(1.5)
## [1] 2
floor(1.5)
## [1] 1
R Strings
A character, or strings, are used for storing text. A string is surrounded by either single quotation marks, or double quotation marks:”hello” is the same as ‘hello’:
Assign a String to a Variable
str <- “Hello”
str # print the value of str
## [1] “Hello”
However, note that R will add a “\n” at the end of each line break. This is called an escape character, and the n character indicates a new line.
If you want the line breaks to be inserted at the same position as in the code, use the cat() function:
str <- “Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.”
str # print the value of str
## [1] “Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor incididunt\nut labore et dolore magna aliqua.”
str <- “Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.”
cat(str)
## Lorem ipsum dolor sit amet,
## consectetur adipiscing elit,
## sed do eiusmod tempor incididunt
## ut labore et dolore magna aliqua.
String Length
There are many usesful string functions in R.
For example, to find the number of characters in a string, use the nchar() function
stf = “Hello World!”
nchar(stf)
## [1] 12
length(stf)
## [1] 1
stv=c(“babak”,2,3,”hasan”)
nchar(stv)
## [1] 5 1 1 5
length(stv)
## [1] 4
Check a String
Use the grepl() function to check if a character or a sequence of characters are present in a string:
zahra=c(“zahra”,”18″,”safaee”,”isfehan”,”4418″)
grepl(“hr”, zahra)
## [1] TRUE FALSE FALSE FALSE FALSE
length(zahra)
## [1] 5
nchar(zahra)
## [1] 5 2 6 7 4
zahra2 <- “Hello World!”
grepl(“H”, zahra2)
## [1] TRUE
grepl(“Hello”, str)
## [1] FALSE
grepl(“X”, str)
## [1] FALSE
Combine Two Strings
Use the paste() function to merge/concatenate two strings:
zahra1 <- “Hello”
zahra2 <- “World”
paste(zahra1, zahra2)
## [1] “Hello World”
str11=c(“babak”,”abasi”,”aaa”)
str12=c(“hi”,”my”,36)
a=paste(str11, str12)
a
## [1] “babak hi” “abasi my” “aaa 36”
R Booleans / Logical Values
Booleans (Logical Values)
In programming, you often need to know if an expression is true or false.
You can evaluate any expression in R, and get one of two answers, TRUE or FALSE.
When you compare two values, the expression is evaluated and R returns the logical answer:
10 > 9
## [1] TRUE
10 < 9
## [1] FALSE
10 >= 9 # TRUE because 10 is greater than 9
## [1] TRUE
10 == 9 # FALSE because 10 is not equal to 9
## [1] FALSE
10 <= 9 # FALSE because 10 is greater than 9
## [1] FALSE
“babak”==”babak”
## [1] TRUE
“farza”==”zahra”
## [1] FALSE
You can also compare two variables:
a = 10
b = 9
a > b
## [1] TRUE
s=c(2,33,34,55)
w=c(3,44,67)
s<w
## Warning in s < w: longer object length is not a multiple of shorter object
## length
## [1] TRUE TRUE TRUE FALSE
s>w
## Warning in s > w: longer object length is not a multiple of shorter object
## length
## [1] FALSE FALSE FALSE TRUE
R Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
10 + 5
## [1] 15
R divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Miscellaneous operators
R Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
R Assignment Operators
Assignment operators are used to assign values to variables
my_var <- 3
my_var1 = 3
my_var # print my_var
## [1] 3
my_var1
## [1] 3
R Comparison Operators
Comparison operators are used to compare two values:
R Logical Operators
Logical operators are used to combine conditional statements:
R Miscellaneous Operators
Miscellaneous operators are used to manipulate data:
END(1-1)