R è spesso utilizzato per l'elaborazione statistica e la presentazione grafica dei dati, al fine di analizzarli e visualizzarli.
"Hello World!"
5 + 5
plot(1:10)
"Ciao Mondo from R!"
25
5 + 5
print("Ciao Mondo from carlomainardi.com!")
for (x in 1:10) {
print(x)
}
# "Good morning!"
"Good night!"
name <- "John"
name # output "John"
age <- 40
age # output 40
text <- "awesome"
paste("R is", text)
text1 <- "R is"
text2 <- "awesome"
paste(text1, text2)
num1 <- 5
num2 <- 10
num1 + num2
num <- 5
text <- "Some text"
num + text
# Assign the same value to multiple variables in one line
var1 <- var2 <- var3 <- "Orange"
# Print variable values
var1
var2
var3
# Legal variable names:
myvar <- "John"
my_var <- "John"
myVar <- "John"
MYVAR <- "John"
myvar2 <- "John"
.myvar <- "John"
# Illegal variable names:
2myvar <- "John"
my-var <- "John"
my var <- "John"
_my_var <- "John"
my_v@ar <- "John"
TRUE <- "John"
Nella programmazione, il tipo di dati è un concetto importante. Le variabili possono memorizzare dati di tipi diversi e tipi diversi possono svolgere funzioni diverse. In R, le variabili non devono essere dichiarate con un tipo particolare e possono anche cambiare tipo dopo essere state impostate:
my_var <- 30 # my_var is type of numeric
my_var <- "Sally" # my_var is now of type character (string)
my_var # print my_var
# numeric
x <- 10.5
class(x)
# integer
x <- 1000L
class(x)
# complex
x <- 9i + 3
class(x)
# character/string
x <- "R is exciting"
class(x)
# logical
x <- TRUE
class(x)
x <- 10.5 # numeric
y <- 10L # integer
z <- 1i # complex
x <- 10.5
y <- 55
# Print values of x and y
x
y
# Print the class name of x and y
class(x)
class(y)
x <- 1000L
y <- 55L
# Print values of x and y
x
y
# Print the class name of x and y
class(x)
class(y)
x <- 3+5i
y <- 5i
# Print values of x and y
x
y
# Print the class name of x and y
class(x)
class(y)
as.numeric()
as.integer()
as.complex()
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
y
# print the class name of a and b
class(a)
class(b)
In R è possibile utilizzare gli operatori per eseguire comuni operazioni matematiche sui numeri. L'operatore + viene utilizzato per sommare due valori. L'operatore - viene utilizzato per la sottrazione.
max(5, 10, 15)
min(5, 10, 15)
sqrt(16)
abs(-4.7)
ceiling(1.4)
floor(1.4)
Le stringhe vengono utilizzate per memorizzare testo. Una stringa è racchiusa tra virgolette singole o doppie. "hello"è lo stesso di 'hello'.
str <- "Hello"
str # print the value of str
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
str <- "Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."
cat(str)
str <- "Hello World!"
nchar(str)
str <- "Hello World!"
grepl("H", str)
grepl("Hello", str)
grepl("X", str)
str1 <- "Hello"
str2 <- "World"
paste(str1, str2)
str <- "We are the so-called \"Vikings\", from the north."
str
cat(str)
\\ | Backslash |
\n | New Line |
\r | Carriage Return |
\t | Tab |
\b | Backspace |
10 > 9 # TRUE because 10 is greater than 9
10 == 9 # FALSE because 10 is not equal to 9
10 < 9 # FALSE because 10 is greater than 9
a <- 10
b <- 9
a > b
a <- 200
b <- 33
if (b > a) {
print ("b is greater than a")
} else {
print("b is not greater than a")
}
10 + 5
10 - 5
2.5 * 1.2
2 ^ 5 # same as 2*2*2*2*2
5 %% 2
15 %/% 2
# the integer divison (%/%) rounds the result down to the nearest whole number
my_var <- 3
my_var <<- 3
3 -> my_var
3 ->> my_var
my_var # print my_var
5 == 5 # TRUE because 5 is equal to 5
5 == 3 # FALSE because 5 is not equal to 3
my_function <- function() {
print("Hello World!")
}
my_function() # call the function named my_function