Вы находитесь на странице: 1из 3

# R BASICS

# The pound sign is used to signify a comment: text to the right of # is


ignored by R
# To execute a command press ctrl + enter (cmd + enter, if you are a Mac
user)
2 + 3 # addition
2 - 3 # subtraction
2 * 3 # multiplication
2 / 3 # division
2 ^ 3 # exponentiation
2 ** 3 # exponentiation
4 ^ 2 - 3 * 2 # combining several arithmetic operations
(4 ^ 2) - (3 * 2) # brackets can be used to manipulate the order of
evaluation
4 ^ (2 - 3) * 2
## Select
( 4 + 3 )
4 + 3 ^ 2
-3 - ( -8
( 3 * 2 ^

several lines to evaluate them at once


^ 2
) + 5 * 6
2 - 4 * 3 ^ 2 ) / 9

## The results of an operation can be given name (or assigned to a


variable) and reused in further calculations
## Use the lef-poinint arrow <- as an assignment operator
a <- ( 4 + 3 ) ^ 2 # Reads: a gets the value ( 4 + 3 ) ^ 2
b <- 4 + 3 ^ 2
a # print the result
b
# CAREFUL: R is case-sensitive: "A" is not the same as "a"
A
# The variable names do not have to be single letters:
MyVar <- 2 ^ (3 ^ 2)
MyVar
# Operations on the defined variables
a + b
a - b
a * b
a / b
a * b - MyVar
MyVar / a + b
# With defined variables we can defined still new ones:
d <- a - b
i <- MyVar - a * b
## WORKSPACE
# Use ls() or objects() to list all the objects that you created during a
session
ls()
objects()
# Use rm() to remove objects from the workspace
rm(a)
ls()

rm(d, i)
ls()
# If you want to remove all the objects from the workspace, you can do it
by pressing "Clear all" button in the north-east panel of RStudio by typing
the following command:
rm( list = ls(all = TRUE) )
# Workspace can be saved as a file and then loaded:
a <- ( 4 + 3 ) ^ 2 # Reads: a gets the value ( 4 + 3 ) ^ 2
b <- 4 + 3 ^ 2
MyVar <- 2 ^ (3 ^ 2)
save.image("~/Desktop/MyWorkspace.RData")
rm(list = ls(all = TRUE))
load("~/Desktop/MyWorkspace.RData")
##
##
##
##
c(

NUMERIC VECTORS
Single calues can be combined into larger structures
The simples such structure is a numeric vector
Combine individual numbers into a vector using the function c()
1, 2, 3, 4, 5 )

# Preferences for the season of the year in America


# data taken from website http://awp.diaart.org/km/surveyresults.html
season <- c( 15, 26, 26, 33 )
season
# vector elements can be given names
names(season) <- c( "winter", "spring", "summer", "fall")
season
# The list of names is a vector as well, a character vector whose elements
are enclosed in qutoation marks. If you enclose numbers in quotation marks,
they will be treated as characters
seas <- c( "15", "26", "26", "33" )
seas * 2
# An alternative way of naming vator elements
season <- c( winter = 15, spring = 26, summer = 26, fall = 33 )
season
# Converting percentages to proportions
season1 <- season / 100 # each element of vector is divided by 100
season1
# Changing proportions to frequencies
season2 <- season1 * 1000
season2
# The sum of elements
sum(season1)
sum(season2)
# Unique elements
unique(season1)
unique(season2)
# The length of the vector
length(season1)
length(season2)

# The smallest element of the vector


min(season1)
min(season2)
# The largest element of the vector
max(season1)
max(season2)
## MORE ON FUNCTIONS
# Functions with more than argument
log(100)
log(100, base = 10)
log(100, 10)
help( pie )
?( pie )
pie( season )
pie( season, main = "Preferred season" )
barplot( season )
barplot( season, main = "Which season
barplot( season, main = "Which season
barplot( season, main = "Which season
'green', 'white', 'red') )
barplot( season, main = "Which season
'green', 'white', 'red'), names.arg =
"Autumn") )
barplot( season, main = "Which season
'green', 'white', 'red'), names.arg =
"Autumn"), horiz = TRUE )
colors()

do you like most?" )


do you like most?", col = "blue" )
do you like most?", col = c("blue",
do you like most?", col = c("blue",
c("Winter", "Spring", "Summer",
do you like most?", col = c("blue",
c("Winter", "Spring", "Summer",

# 1. Go to website http://awp.diaart.org/km/surveyresults.html
# 2. Find results concerning the preferred type of art in Iceland
# 3. Use the percentages reported on that website to create a numeric
vector called art and name each element of that vector accordingly
# 4. Find information on the number of participants in the survey. Use this
information to change the percentages into frequencies
# 5. Check that the frequencies sum to 1,000
# 6. How many subjects would be in the most preferred category if the
sample size were 500?
# 7. Create a pie chart showing art preferences in Iceland. Add a title
# 8. Create a barplot. Color each bar differently. Add a title

Вам также может понравиться