CLASS 1 (Variables and Vectors)
CLASS 1 (Variables and Vectors)
I/O functions
Dr. Fuensanta Medina Domínguez
Objective
Variables
Vectors
I/O functions
Exercises
2
What elements are involved in a program?
PROCESS
INPUT DATA OUTPUT DATA
Software/System/
Program/Application
JAMBOARD:
Group 82
Group 83
3
Variable & Data Structure
Hello 5 45434432D
Grades of students:
5 2 4 9 4 8 7 6
Students
Luis
Rosa
Raquel
David 4
R: Variable
Variable: it is a memory space reserved to store a value
that corresponds to a type of data supported by the
programming language.
A variable represents a data
Ex: number of pizza, ingredients, number of tickets, etc…
Variable Name:
start with a letter followed by more letters, digits or the dot (.) Or
underline (_) characters
num_pizza, num_tickets, date, hour, etc….
When the program is executed it is replaced by the value of the data
R: Variable
Name_Variable <- value
To assign value
Name_Variable = value
Grades of students:
5 2 4 9 4 8 7 6
Students
Luis
Rosa
Raquel
David 11
Data Structure: Vector
A vector is a set of same type of data: numeric, logic, text,
etc. if there is a vector with
letters and numbers e.g.
To create a vector, use: A/8/6/B; it is a text
charater vector
variable “=“ “c” + “(“ + elements sepate with coma + “)”
a=c(1,3,5)
b=c(2,4,6)
d=c(a,b) d=c(b,a)
[1] 1 3 5 2 4 6 [1] 2 4 6 1 3 5
Data Structure: Vector
To select elements of a vector, you have to use the position
of the element in the vector:
>d=c(1,3,5,2,4,6)
>d[2] d[c(1,3,6)]
[1] 3 [1] 1,5,6
Data Structure: Vector
Other example:
A=1
B=3
C=5
p=c(A,B,C)
[1] 1 3 5
POSITION
D= p[B] = P[3]= 5
[1] ???????
Data Structure: Vector
Other example:
A=1
B=3
C=5
p=c(A,B,C)
[1] 1 3 5
D= p[B]
[1] 5
Data Structure: Vector
◼ To eliminate same element of a vector, you have to use:
1/3/5
variable “=“ vector_name + “[“ + “-” + position + ”]”
or p= p [-2] -> 1/5
>d=c(1,3,5,2,4,6)
>d[-2] d[-c(1,3,6)]
[1] 1 5 2 4 6 [1] 3 2 4
Data Structure: Vector
Some functions with vectors
A=c(1,3,5,2,4,6)
>sum(A) Sum of vector elements
[1]21
>min(A) The least number of the vector
[1]1
>max(A) The greatest number of the vector
[1]6
>length(A) Numbers of vector elements
[1]6
>range(A) The least and greatest number of the
[1]1 6 vector
>mean(A) The mean of vector elements
[1]3.5
>sort(A) The vector is ordered into ascending
[1]1 2 3 4 5 6 order 18
Data Structure: Vector
◼ To select with a logical condiction:
>d=c(1,3,5,2,4,6)
>d>3
[1] FALSE FALSE TRUE FALSE TRUE TRUE
# if d is greater than 3
>d[d>3]
[1] 5 4 6
19
Logical Operators
Value OPERATOR Value Result
FALSE AND FALSE FALSE
FALSE AND TRUE FALSE
TRUE AND FALSE FALSE
TRUE AND TRUE TRUE
20
Data Structure: Vector
Logical operators:
>d=1:5
>d>1 # if d is greater than
1
[1] FALSE TRUE TRUE TRUE TRUE
>d < 5 # if d is less than 5
[1]TRUE TRUE TRUE TRUE FALSE
>d>1 & d<5
[1] FALSE TRUE TRUE TRUE FALSE
>d=1:5
>d==3 # d as same as 3
[1] FALSE FALSE TRUE FALSE FALSE
22
Exercise 3: Vector
Create a vector with these elements: 2,17,15,7,11,3,8,19
Calculate:
the maximum
the minimum
the length of the vector
the first element of the vector
the last element of the vector
the sum of the vector elements
the range
average
order from lowest to highest vector elements
Exercise 3: Vector
order from highest to lowest vector elements
the square of each vector element
the accumulated
Is each element of the vector greater than 5?
show each of the values of vector elements that is greater than 5.
select the first three elements of the vector
select the first, third and fourth element
exclude the second, third and sixth element
exclude the 4th element of the vector
assign the value 6 to the third vector element
assign the value 8 and 2 to the third and fifth vector element
Output Data
Show data on screen during the execution of a program: print() ; cat()
>print("Hello")
#Execution; the user will read:
Hello
Input Data
User can enter a value in the console during execution using different
functions: readline() ; scan()
readline(prompt = "")
Example:
colour=readline(prompt = "Write a colour: ")
Write a colour:
> colour
[1] "red" 26
Input
scan (file = "", what = double(), nmax = -1, n = -1, sep = "", quote = if(identical(sep, "\n")) "" else
"'\"", dec = ".", skip = 0, nlines = 0, na.strings = "NA", flush = FALSE, fill = FALSE, strip.white =
FALSE, quiet = FALSE, blank.lines.skip = TRUE, multi.line = TRUE, comment.char = "", allowEscapes
= FALSE, fileEncoding = "", encoding = "unknown", text, skipNul = FALSE)
Example:
> colour=scan(, what=character(),2)
#The user will read
1:
#User will write: red
1: red
#The user will read
> colour=scan(, what=character(),2)
1: red
2:
#User will write: blue
> colour=scan(, what=character(),2)
1: red
2:blue
Read 2 items
> colour
29
Objective
Variables
Vectors
I/O functions
Exercises
30
Exercise 1
Calculate
1+2(3+4)
43+32+1
(4+3)(2+1)
√(1+2/3+4)2
Real division:(4+3)/(2+1)
Module:(4+3)/(2+1)
Whole division:(4+3)/(2+1)
Exercise 2:
Calculate
1. A=5 ; B=6; C= A+B ; A=A+B; D=A+B.
What are the results of variables A, B,C and D?
2. A=5; B=2; B=A*B; C=B;A=C
What are the results of the variables A, B and C?
Exercise3: Vector
Create a vector with these elements: 2,17,15,7,11,3,8,19
Calculate:
the maximum
the minimum
the length of the vector
the first element of the vector
the last element of the vector
the accumulated
the range
average
order from lowest to highest vector elements
Exercise3: Vector
order from highest to lowest vector elements
the square of each vector element
the sum of the vector elements
Is each element of the vector greater than 5?
show each of the values of vector elements that is greater than 5.
select the first three elements of the vector
select the first, third and fourth element
exclude the second, third and sixth element
exclude the 4th element of the vector
assign the value 6 to the third vector element
assign the value 8 and 2 to the third and fifth vector element
Exercise 4:
Calculate
a) Insert two numbers by Keyboard: 5 and 10. Sum both
numbers and insert the result in a variable called C. Show
the value of variable C with this message: “The result is
«value».”
b) Insert three variables with the numbers: 2, 5 and 10.
Perform the following operation with variables and insert
the result in the variable C, C=2*5*10. Show the value of
variable C with this message: “The result is «value».”
Exercise 5:
- Ask to the user the next data and show the next message with data:
- Please, let me know the next data:
Insert your name: JUAN
Insert your age: 20
Insert your email: [email protected]
36