-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript1.R
More file actions
84 lines (66 loc) · 1.69 KB
/
script1.R
File metadata and controls
84 lines (66 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Simple assignment operators
# These three operations do the same thing:
x = "a"
x <- "a"
"a" -> x
c
#vectors:
# - one or more elements
# - one data type
v0 = 1:4
v0
#everything is a vector
#weak typed language, can check class type with:
class(x)
class(v0)
is.vector(x)
is.vector(v0)
#getting the length
length(v0)
v1 = c(1,2,3,8,4) #c() is the concatenation function, makes a vector
v1[3:5]
v1[c(1,3,4)]
v1[c(T,F,F,F,T)]
#matrix examples
m1 = matrix( data=1:12, nrow=3 )
m2 = cbind( 1:10, (1:10)^2 )
class(m2)
#finding the dimensions:
dim(m2)
#naming dimensions:
colnames(m2)<-c("James","Ted")
#gotchya:
test1 = m2[,"James"] #The output from this is a vector. To keep the matrix data type, use drop=FALSE
#error
test1[1:2,"James"]
#the type changed:
print(test1)
is.vector(test1)
#the correct way
test2 = m2[,"James",drop=FALSE]
print(test2)
test2[1:2,"James"]
#basic plotting
plot(x=1:10,y=10:1)
plot(m2)
#if the columns were not named:
colnames(m2)<-NULL
plot(m2)
#name the dimensions
plot(m2, xlab="j numbers", ylab="t numbers", main="Plot 2")
sdat = read.table("./injuriesVsWrithingTimeVsRegion.txt", header=T, sep="\t") #see also read.csv()
#ggplot example:
install.packages('ggplot2')
library('ggplot2')
ggplot(data=sdat, aes(x=Injuries, y=Time))+
geom_point()
#map data to visual elements
ggplot(data=sdat, aes(x=Injuries, y=Time, color=Region))+
geom_point()
sdat$Region <- factor(sdat$Region, levels=c("Australia", "Middle.East","Africa", "Asia",
"C.America","N.America","S.America", "Europe"))
ggplot(data=sdat, aes(x=Injuries, y=Time, label=Team))+
stat_smooth()+
geom_point()+
geom_text()+
ggtitle("Number of injuries vs time spent writhing on the ground")