-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoding Class 1.R
More file actions
122 lines (81 loc) · 1.96 KB
/
Coding Class 1.R
File metadata and controls
122 lines (81 loc) · 1.96 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ting
box <- 9
box
class(box)
name <- "peter"
class(name)
#creating a vector (variable with values)
a_vector<-c(2,3,4)
cats <- c(2,3,4)
cats
#check length of vector
length(a_vector)
#make it a variable
length_a_vector <- length(a_vector)
class(length_a_vector)
#multiplying vector
a_new_vector <- a_vector * 3
#vector with both numbers and strings
a_new_vector <- c(3,a_new_vector, "car")
class(a_new_vector)
#combining vectors
group1 <- c(cats,a_vector)
group1
a_vector + 3
a_vector
#Dividing a string
name + 4
#vectors are indexed. Values can be retrieved.
a_vector[1]
#remove values from vectors
a_new_vector[-1]
a_new_vector[-c(1,2)]
#remove variable from environment
rm(box)
#for R to remember what we are doing we need to make it a variable
a_new_vector2 <- a_new_vector[-c(1,2)]
#remove value
a_new_vector <- a_new_vector[-5]
#change class
a_new_vector <- as.numeric(a_new_vector)
sum(a_new_vector)
#Exercise 1
names <- c('Lasse','Kasper','Kazik')
siblings <- c(1,2,3)
names + 2
siblings + 2
class(names)
class(siblings)
sum(siblings)
siblings[1] * siblings[2]
#Exercise 2
#creating data frame with names and siblings
df <- data.frame(names,siblings)
#create a new vector for gender
gender <- c('male','male','male')
#add gender to data frame
df$gender <- c(gender)
df
#
df[,2]
df["siblings"]
df$siblings
#adding a row first try
df <- rbind(df,c('Freya',2,'female'))
#error message: "names" was a factor. Needs to be changed.
df$names <- as.character(df$names)
#adding a row second try
df <- rbind(df, c('Freya',2,'female'))
#mean of siblings - need to change class of the values first.
df$siblings <- as.numeric(df$siblings)
mean(df$siblings)
#add a column of actual siblings
actual_siblings <- c(1,1,2,2)
df$actual_siblings <- c(actual_siblings)
df
#how much were we off (guessed siblings vs actual)
df$actual_siblings <- as.numeric(actual_siblings)
df[,2] - df[,4]
siblings_off <- df[,2] - df[,4]
df$siblings_off <- c(siblings_off)
#Exercise 3