Professional Documents
Culture Documents
height<-c(132,151,162,139,166,147,122)
weight<-c(66,71,70,56,61,54,49)
gender<-c("male","female","male","female","female","female","male")
input_data<-data.frame(height,weight,gender)
print(input_data) #you must select all code in order to compile it#
print(is.factor(input_data$gender))
print(input_data$gender)
data <-
c("East","West","East","North","North","East","West","West","West","East","Nor
th")
# Create the factors
factor_data <- factor(data)
print(factor_data)
#Generate levels
v<-gl(3,4,labels=c(1,2,3))
print(v)
#Arrays
vector1<-c(3,4,5)
vector2<-c(10,9,11,12,10,6)
result<-array(c(vector1,vector2),dim=c(3,3,2))
print(result)
#Col.names,row.names
vector<-array(c(c(1,2,3),c(2,3,4),c(3,4,5)),dim=c(3,3))
print(vector)
col.names<-c("COL1","COL2","COL3")
row.names<-c("R1","R2","R3")
result<-
array(c(c(1,2,3),c(2,3,4),c(3,4,5)),dim=c(3,3),dimnames=list(row.names,
col.names))
print(result)
#For loops
v<-LETTERS[1:4]
for (i in v){
print(i)
}
#Nested loops
v1<-LETTERS[1:5]
v2<-LETTERS[1:5]
for (i in v1)
for (j in v2)
{print(c(i,j))}
#repeat + break
v<-c("Help","please")
cnt<-2 #counter
repeat
{
print(v)
cnt<-cnt+1
if(cnt>5){break}}
#Exercise:
#set the counter to 6,and repeat printing until counter gets<=2
cnt<-6
repeat{print(v);cnt<-cnt-1;if(cnt<=2){break}}
#while loop
#Given a vector (1,2,3)print each vector c+i,until the second element is
greater than 7
x<-c(1,2,3)
while(x[2]<=7)
{
x<-x+1
print(x)#this prints one more sequence than permitted
}
#we use instead
x<-c(1,2,3)
while(x[2]<=7)
{
print(x)
x<-x+1
}
print(x+1)
print(seq(22,33))
print(c(1:4))
print(c(seq(10,12),c(1:4)))
print(mean(20:23))
print(sum(10:15))
new.function<-function(a)
{
for(i in 1:a)
{
b<-i^2
print(b)
}
}
new.function(10)
#Next part--functions
new.function<-function(a,b,c){
result<-a*b+c
print(result)
}
new.function(5,3,11)
function2<-function(a,b)
{
result<-a*b+a+b
print(result)
}
function2(2,2)
#misstyped arguments
new.function2<-function(a,b){
print(a^2)
print(a)
print(b)#this will give a missing argument error
}
new.function2(6)
#String manipulations
a<-"Hi";b<-",my name is";c<-"Theodor"
print(paste(a,b,c))
#Formatting
result<-format(23.123451234,digits=5)
print(result)
result2<-format(23.3212,digits=3)
print(result2)
result<-format("Hello",digits=4)
print(result)
#result<-format("Hello"23.1222,digits=4)#unexpected numeric constant in
result<-...
print(result)
result<-format("hello,23.1222",digits=4)
print(result)#prints the whole string because the number is between quotations
# to clear the screen you can either press CTRL+L or cat("\014")or cat("\F")
#Scientific notation
result<-format(c(6,13.1415),scientific=TRUE)
print(result)#it prints with e+01
#the minimum number of digits to the right of the decimal point
result<-format(23.4711,nsmall=3)
print(result)#it prints the number as it is
result<-format(23.47,nsmall=3)
print(result)# it adds one zero
result<-format(6)
print(result)# treats 6 as a string
result<-format(13.7,width=6)
print(result)#uses a 6 width character to print 13.7 which means that the
#string is moved to the right with 2 positions
result<-format("Hello",width=8,justify="c")
print(result)#It is printed centered
result<-format("Hello",width=8,justify="r")
print(result)
#number of characters
print(nchar("Hello,my name is Paul"))
x<-"Hello,how are you?"
print(toupper(x))
print(tolower(x))
result<-substring("Extract",5,7)
print(result)
#Lists
list_data<-list("RED","GREEN",c(10,11,21),TRUE,51.24)
print(list_data)
#Exercise 2
#a)Create a list containing two matrices, defined by nrow and ncol
respectively
list_data<-list(matrix(c(1,2,3,4,5,6),ncol=3),matrix(c(1,2,3,4,5,6),nrow=3))
print(list_data)
#b)create two lists,transform them to vectors and add them after that
list1<-list(1:5)
print(list1)
list2<-list(10:14)
print(list2)
v1<-unlist(list1)
v2<-unlist(list2)
result<-v1+v2
print(result)
#Create pies
x<-c(10,21,31,19)
labels<-c("Athens","London","Tokyo","Moscow")
png(file="cities.png")
pie(x,labels)
dev.off()
png(file="city_title_colours.jpg")
pie(x,labels,main="Cities",col=rainbow(length(x)))
dev.off()
x<-c(21,62,10,53)
labels<-c("Athens","New York","Bucharest","Tokyo")
piepercent<-round(100*x/sum(x),1)
png(file="city_pie_percent.jpg")
pie(x,labels=piepercent,main="City pie chart",col=rainbow(length(x)))
legend("topright",c("Athens","New
York","Bucharest","Tokyo"),cex=0.8,fill=rainbow(length(x)))
dev.off()
#there is no pie3D function inside our rStudio
#To install a package you must do like this:
#install.packages("C:\\Program Files\\R\\R-3.3.2\\plotrix_3.6-
4.zip",repos=NULL,type="source")
#not like this:install.packages("plotrix")
pie3D(x,labels=piepercent)#pie3D is a method of plotrix
#BARCHARTS
H<-c(10,21,22,13,14)
png(file="barchart.png")
barplot(H)
dev.off()
H<-c(10,10,11,11,12)
barplot(H)
#How to create a sequence of repeated elements
c<-rep(2,10)# repeats the number 2 10 times
print(c)
c<-rep(10,2)
print(c)
H<-c(rep(10,2),rep(11,4),rep(12,6))
barplot(H)#displays each occurence of 10,of 11 and of 12
#Connect to MYSQL
#mysqlconnection=dbConnect(MySQL(),user='root',password='your_password',dbname
='people',host="localhost")
#Plots,line charts
v<-c(7,13,28,3,41)
png(file="line_chart.png")
plot(v,type="o")
dev.off()
#with color and label
png(file="line_chart_with_color.jpg")
plot(v,type="o",col="red",xlab="Month",ylab="Rain fall",main="Rain fall
chart")
dev.off()
#Multiple lines chart
v<-c(7,12,28,3,41)
t<-c(14,7,6,19,3)
png(file="line_chart_2_lines.jpg")
plot(v,type="o",col="red",xlab="Month",ylab="Rain Fall",main="Rain fall
chart")
lines(t,type="o",col="blue")
lines(t+1,type="o",col="blue")
lines(v-1,type="o",col="yellow")
dev.off()
#Scatterplots
input<-mtcars[,c('wt','mpg')]
print(head(input))
input<-mtcars[,c('wt','mpg')]
png(file="scatterplot.png")
plot(x=input$wt,y=input$mpg,
xlab="Weight",
ylab="Mileage",
xlim=c(2.5,5),ylim=c(15,30),
main="Weight vs Mileage")
dev.off()
#Scatterplots matrices
#We give the chart file a name
png(file="scatterplot_matrices.png")
pairs(~wt+mpg+disp+cyl,data=mtcars,main="Scatterplot Matrix")
pairs(~wt+mpg+disp,data=mtcars,main="Scatterplot Matrix")
dev.off()
#LINEAR REGRESSIONS
#Example 1
x<-c(151,174,138,186,128,136,179,163,152,131)
y<-c(63,81,56,91,47,57,76,72,62,48)
relation<-lm(y~x)
print(relation)
#Example 2
x<-c(1,2,3)
y<-c(3,5,8)
relation<-lm(y~x)
print(relation)#Least square estimator are used for the two quotients
#b=intercept,x=a,for f(x)=a*x+b
#SUmmary of the relationship
x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
relation <- lm(y~x)
print(summary(relation))
#predict function
x<-c(151,174,138,186,128,136,179,163,152,131)
y<-c(63,81,56,91,47,57,76,72,62,48)
relation<-lm(y~x)
a<-data.frame(x=179)
result<-predict(relation,a)
print(result)
b<-data.frame(x)f
result2<-predict(relation,b)
print(result2)
d<-data.frame(c(128,136))
result3<-predict(relation,d)
print(result3)