Difference Between Print and Paste Function?

You might also like

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 6

Storing values in R

• variable names are case sensitive


• value of a variable can be seen in the environment tab
• assign value with “ <- ” – do not put e.g. name ← mao ; put name ← “mao”
• see output in the console tab

→ Ctrl R line by line gives the same output – else error


Data type Description
Character Text/string → “R string”
Double Decimal number e.g. 3.14
Integer Whole number
Boolean Logical value → TRUE/FALSE

• R automatically determines the variable data type → revealed by using the typeof() function
• numeric variables are by default created as a double data type unless assigned an integer
type by the letter L (e.g. number = 5L) – for more efficient data storage
• use the is.character() function to test data type → returns a boolean value – true/false
• assign boolean values via TRUE/FALSE or T/F

difference between print() and paste() function?


title <- "Mao"
result <- paste("type of title:", typeof(title))
print(result)
Probably, function paste is one of the most used function in R. The objective of this function is concatenate a
series of strings.
# First example

paste("file", "number", "32")

[1] "file number 32"

# Second example

paste("file", "number", "32", sep = "_")

[1] "file_number_32"

(CHARACTER VALUE OUTPUT)

STORING MULTIPLE VALUES

• vector = variable that can contain multiple values – each value is contained within an element of the
vector
• assign multiple values to a variable by using the “combine function” → c()

month =c("Jan","Feb","Mar") → environment tab will output:

- does not use “< -” to store values


- first value stored would be stored in element one, etc
- vectors are addressed by placing the element # in [] ==> e.g. month[1] would retrieve the value “Jan”
- assign new values by using e.g. month[ 3 ] = “March” → replaces Mar with March
- using “length()” will output the number of elements in the vector ==> e.g. length(month) will output 3
[paste(length()) outputs “3” while print(length()) outputs 3 → not a string]
- vectors can be dynamically expanded → e.g. when assigning a new value/element
- each vector can ONLY contain values of the SAME data type – when in doubt use typeof() to see what data
type the vector is
- use is.vector() to find out if a variable is a vector

HOW TO STORE MIXED DATA TYPES → USING THE “List” structure

- e.g. data < - list(12, 3.14, “Mike”, TRUE) ==> BACK TO Using the <-
- lists are NOT flexible → cannot dynamically add on extra elements unlike vectors
- can use c() combine function to combine 2 lists

e.g. if you want to extend a list


sales<-list(Jan=1500, Feb=1300, Mar=1200)
sales<-c(sales, list(Apr=1800)) ← combining the sales with a new list with april and redefining it as the NEW
list)

can name each element in a list: data<- list(dozen=12, pi=3.14, name= “Mao”, flag=TRUE) so that if you use the
$ operator you can retrive the specific value ==> data$name will retrieve the value “Mao” → this is called the
KEY=VALUE pairs

names() function → retrieves all the KEYS


data1<-list(number=1,decimal=2.1,names="Jia", text=FALSE)
names(data1) OUTPUTS [1] "number" "decimal" "names" "text"

unlist(data1) OUTPUTS > unlist(data1)


number decimal names text
"1" "2.1" "Jia" "FALSE"
> unlist(data1, use.names=FALSE)
[1] "1" "2.1" "Jia" "FALSE"

IF THE VALUES IN THE LIST ARE NUMERICAL, MATHEMATICAL FUNCTIONS CAN BE APPLIED

PLOTTING STORED VALUES


plot() function produces a scatter plot of 2 vectors
x<-c(1,3,5,7,9)
y<-c(2,4,6,8,10)
plot(x,y)
outputs the graph with only data points; plot(x,y, type="o") includes the lines connecting the datapoints

qtr.1<-list(Jan=1500, Feb=1300, Mar=2400)


qtr.2<-list(Apr=1800, May=1700, Jun=2800)
qtr.3<-list(Jul=3100, Aug=3800, Sep=3200)
qtr.4<-list(Oct=2600, Nov=2200, Dec=2400)
year<-unlist(c(qtr.1,qtr.2,qtr.3,qtr.4))
print(year)
output - Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1500 1300 2400 1800 1700 2800 3100 3800 3200 2600 2200 2400
(combining the lists) → cannot just add dynamically like a vector.

plot(year, type="o", col="Blue", pch=15,ann=FALSE, axes=FALSE) OUTPUTS


- the variable “pch” changes the dots to different types – e.g. crosses, circles, triangles etc. → it is called a point
character, and has the range 0-25 for different types.
- “ann” when put to TRUE gives the names of the axes

as of plot(year, type="o", col="Blue", pch=4,ann=TRUE, axes=FALSE)

the axes are not yet defined, and hence the y-axis just shows the name of the list, while x-axis is “index”

TO SPECIFIY THE RANGE AND ANNOTATION FOR THE X-AXIS:


axis(1,at1:12, lab=c(names(year)))
axis(2) ← allowing R to automitically annotate the Y axis

IF BOTH plot(year, type="o", col="Blue", pch=4,ann=TRUE, axes=TRUE) and axis(1, at=1:12,


lab=c(names(year))) are inputted and ran, the output will be an overlap of the default x-axis and the desired one:

if we just input plot(year, type="o", col="Blue", pch=4) without defining the ann or axes → by default their
value will be set to TRUE
combining:

plot(year, type="o", col="Blue", pch=4)


axis(1, at=1:12, lab=c(names(year)))
axis (2)
will output
(remembering that the names() function retrieves all the KEYS of the list in the order they appear – good for
labelling the X axis in graphs)
12--> there are 12 values in the original data/list

and hence we will need to add the title and make a box

title(xlab="Month", ylab="$", main= "Yearly Sales", col.main="Red")


box()

day<-list(monday=1,tuesday=2,wednesday=3,thursday=4)
plot(days, type="o", col="Red", pch=20, ann=FALSE, axes=FALSE)
results in an error as the plot function cannot form a plot based on LISTS – has to be a
vector → e.g. of numerical values.
The vector needs to appear in the values table in the environment section →

hence, we need to use the unlist() function to form a vector from the list [remembering
that the unlist functions returns a vector of all keys and values, and names can be
explicitly ignored by including a use.names=FALSE argument.]

days<-unlist(day)
axis(1, at=1:4, lab=c(names(day))) ← the at=1:x , x depends on the no. of values
IF WE had typed:

days<-unlist(day, use.names=FALSE) → aka removing the


monday/tuesday/wednesday/thursday and only saving the values of 1,2,3,4 → the graph
output will not be able to have the names on the x-axis

axis(1, at=1:4, lab=c(names(day))) → x-axis will have mon/tues/wed because the names
is defined within the variable “day” VS
axis(1, at=1:4, lab=c(names(days))) → x-axis will NOT have mon/tues/wed because the
vector days has been defined to exclude the keys (mon/tues/etc)

You might also like