DEV Community

sc0v0ne
sc0v0ne

Posted on

Statistics with R - Introduction to R Language and Statistics

What is statistics?

Statistics is a science, where we use information from our world to provide answers to the questions created. But how is it science? Using statistics is not enough to just collect data (information), the data needs to be trained, understood and processed to obtain a final result. This entire process until reaching the result brings a learning of great value. Where at each point that you manipulate it can bring a different result. I don't think it's that bad, it can be a challenge, having to test and evaluate again until reaching an effective result.

Within statistics there are several visual, mathematical, data collection and software tools. To be able to address various real-world problems. The most incredible thing about statistics is that it is interdisciplinary. Every area that you think will have something statistical connected to it. Starting with the simplest thing, the TV news shows the weather forecast. The weather is constantly changing, but through data collection, there are already known patterns used to forecast the weather for the next day. Going further, understanding space, for example, supernovae, using what is already known to measure cosmic distances.

Why should we study statistics?

By learning statistics, we can understand the biggest problems in our world, where observation alone does not provide answers. By collecting and studying data, we can find more data that we cannot visualize without proper processing.

Why use the R language?

The R language is a programming language focused on statistics. It has a wide variety of algorithms and functions to apply to various statistical problems. It is possible to explore data sets, process data, visualize and more diverse resources available in its documentation. Furthermore, the project is open source, meaning that any human being can consult the code and assist in the development and evolution of the project.

Comments

Single-Line Comments in R

# Hey !!!!!
Enter fullscreen mode Exit fullscreen mode

Multi-line Comments in R

# Hey !!!
# Hello !!!
# Here !!!!!!
Enter fullscreen mode Exit fullscreen mode

Variables

Variable Assignment and Output

simple_text <- "Python or R ?"
[1] "Python or R ?"
Enter fullscreen mode Exit fullscreen mode

Data Types

String Assignment and Structure

example_text <- "Python"
str(example_text)
[1] "Python"
Enter fullscreen mode Exit fullscreen mode

Integer Assignment and Printing

number_dogs <- 15
number_cats <- 10

print(number_dogs)
[1] 15
print(number_cats)
[1] 10
Enter fullscreen mode Exit fullscreen mode

Integer Structure

print(str(number_dogs))
int 15
print(str(number_cats))
int 10
Enter fullscreen mode Exit fullscreen mode

Double Assignment and Structure

salary <- 1300.33
bonus <- 112.67

print(str(salary))
num 1300.33
print(str(bonus))
num 112.67
Enter fullscreen mode Exit fullscreen mode

Class of Double

class(salary)
[1] "numeric"
class(bonus)
[1] "numeric"
Enter fullscreen mode Exit fullscreen mode

Convert Double to Integer

to_int <- as.integer(bonus)
[1] 112
Enter fullscreen mode Exit fullscreen mode

Rounding Numbers

round(bonus)
[1] 113
round(salary)
[1] 1300
Enter fullscreen mode Exit fullscreen mode

Convert Double to Character

to_char <- as.character(salary)
[1] "1300.33"
Enter fullscreen mode Exit fullscreen mode

Print Double and Character

print(salary)
[1] 1300.33
print(to_char)
[1] "1300.33"
Enter fullscreen mode Exit fullscreen mode

Logical, Arithmetic, and Relational Operators

Multiplication

a <- 3
b <- 10

print(a * b)
[1] 30
Enter fullscreen mode Exit fullscreen mode

Division

a <- 3
b <- 10

print(a / b)
[1] 0.3
Enter fullscreen mode Exit fullscreen mode

Addition

a <- 3
b <- 10

print(a + b)
[1] 13
Enter fullscreen mode Exit fullscreen mode

Subtraction

a <- 3
b <- 10

print(a - b)
[1] -7
Enter fullscreen mode Exit fullscreen mode

Equality Check

"a" == "b"
[1] FALSE
Enter fullscreen mode Exit fullscreen mode

Equality Check

1 == 1
[1] TRUE
Enter fullscreen mode Exit fullscreen mode

Logical Class

logic_ <- TRUE

class(logic_)
[1] "logical"
Enter fullscreen mode Exit fullscreen mode

Multiplication with Logical False

FALSE * 2
[1] 0
FALSE * 100
[1] 0
FALSE * 300
[1] 0
Enter fullscreen mode Exit fullscreen mode

Multiplication with Logical True

TRUE * 2
[1] 2
TRUE * 100
[1] 100
TRUE * 300
[1] 300
Enter fullscreen mode Exit fullscreen mode

Greater Than Check

2 > 5
[1] FALSE
Enter fullscreen mode Exit fullscreen mode

Equality Check

2 == 5
[1] FALSE
Enter fullscreen mode Exit fullscreen mode

Power Calculation

5^2
[1] 25
Enter fullscreen mode Exit fullscreen mode

Vectors, Matrices, Dataframe

Create and Display Vector

group_numbers <- c(1,2,3,4,5,6,7,8,9,10)
[1] 1 2 3 4 5 6 7 8 9 10
Enter fullscreen mode Exit fullscreen mode

Vector Multiplication

group_numbers * 5
[1]  5 10 15 20 25 30 35 40 45 50
Enter fullscreen mode Exit fullscreen mode

Vector Power

group_numbers ^ 2
[1]   1   4   9  16  25  36  49  64  81 100
Enter fullscreen mode Exit fullscreen mode

Vector Division

g <- group_numbers / 2
[1] 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
Enter fullscreen mode Exit fullscreen mode

Class of Vector

class(g)
[1] "numeric"
Enter fullscreen mode Exit fullscreen mode

Create and Display Vector with Integers

x <- c(133, 45, 23, 12, 1)
typeof(x)
[1] "double"
length(x)
[1] 5
Enter fullscreen mode Exit fullscreen mode

Create and Display Mixed Type Vector

x <- c(33, 132.4, TRUE, "Python", FALSE)
[1] "33"     "132.4"  "TRUE"   "Python" "FALSE"
typeof(x)
[1] "character"
Enter fullscreen mode Exit fullscreen mode

Create and Display Named Vector

x <- c("first_name"='Xeroxnildo', "last_name"='Carlomeu', "year"=97)
names(x)
[1] "first_name" "last_name"  "year"      
x["first_name"]
[1] "Xeroxnildo"
x["last_name"]
[1] "Carlomeu"
x["year"]
[1] "97"
Enter fullscreen mode Exit fullscreen mode

Sequence with Increment

seq(1, 40, by=0.7)
[1]  1.0  1.7  2.4  3.1  3.8  4.5  5.2  5.9  6.6  7.3  8.0  8.7  9.4 10.1 10.8 11.5 12.2 12.9 13.6 14.3 15.0 15.7 16.4 17.1 17.8 18.5 19.2 19.9 20.6 21.3 22.0 22.7 23.4 24.1 24.8 25.5 26.2 26.9 27.6 28.3 29.0 29.7 30.4 31.1 31.8 32.5 33.2 33.9 34.6 35.3 36.0 36.7 37.4 38.1 38.8 39.5 40.2
Enter fullscreen mode Exit fullscreen mode

Sequence with Length

seq(1, 10, length.out=6)
[1]  1.0  2.8  4.6  6.4  8.2 10.0
Enter fullscreen mode Exit fullscreen mode

Decision and repetition structures

Variable Assignment and Printing

question <- 'Python is better than R ?'
print(question)
[1] "Python is better than R ?"
Enter fullscreen mode Exit fullscreen mode

Simple If Statement

x <- TRUE
if(x){
   print("True")
}
[1] "True"
Enter fullscreen mode Exit fullscreen mode

If-Else Statement

x <- -100
if(x > 0){
   print("TRUE")
} else {
   print("FALSE")
}
[1] "FALSE"
Enter fullscreen mode Exit fullscreen mode

For Loop with Conditional Increment

x <- c(33,12,6,2,1,13,154)
count <- 0
for (val in x) {
    if(val %% 2 == 0)  count = count+1
}
print(count)
[1] 4
Enter fullscreen mode Exit fullscreen mode

My Latest Posts


Favorites Projects Open Source


About the author:

A little more about me...

Graduated in Bachelor of Information Systems, in college I had contact with different technologies. Along the way, I took the Artificial Intelligence course, where I had my first contact with machine learning and Python. From this it became my passion to learn about this area. Today I work with machine learning and deep learning developing communication software. Along the way, I created a blog where I create some posts about subjects that I am studying and share them to help other users.

I'm currently learning TensorFlow and Computer Vision

Curiosity: I love coffee

Top comments (0)