01_lab_R_learning--1-
.docx
keyboard_arrow_up
School
New York University *
*We aren’t endorsed by this school
Course
MISC
Subject
Mathematics
Date
Apr 3, 2024
Type
docx
Pages
9
Uploaded by htk236
Lab 01 R Learning
Kelly
2020-03-26
Logical Operators:
1.
Use logical operations to get R to agree that “two plus two equals 5” is FALSE.
2
+
2
==
5
## [1] FALSE
2.
Use logical operations to test whether 8 ^ 13 is less than 15 ^ 9.
8
^
13
<
15
^
9
## [1] FALSE
Variables:
3.
Create a variable called potato whose value corresponds to the number of potatoes you’ve eaten in the last week. Or something equally ridiculous. Print out the value of potato.
potato=
88
potato
## [1] 88
4.
Calculate the square root of potato using the sqrt() function. Print out the value of potato again to verify that the value of potato hasn’t changed.
sqrt
(potato)
## [1] 9.380832
potato
## [1] 88
5.
Reassign the value of potato to potato * 2. Print out the new value of potato to verify that it has changed.
potato =
potato
*
2
potato
## [1] 176
6.
Try making a character (string) variable and a logical variable . Try creating a variable with a “missing” value NA. You can call these variables whatever you would like. Use class(variablename) to make sure they are the right type of variable.
city =
"age"
class
(city)
## [1] "character"
areyoufat =
FALSE
class
(areyoufat)
## [1] "logical"
email=
NA
class
(email)
## [1] "logical"
Vectors:
7.
Create a numeric vector with three elements using c().
a=
c
(
3
,
4
,
5
)
a
## [1] 3 4 5
8.
Create a character vector with three elements using c().
myfavfood=
c
(
"icecream"
,
"candy"
,
"cake"
)
myfavfood
## [1] "icecream" "candy" "cake"
9.
Create a numeric vector called age whose elements contain the ages of three people you know, where the names of each element correspond to the names of those people.
age=
c
(
25
,
55
,
60
)
names
(age)=
c
(
"kelly"
,
"stanley"
,
"cora"
)
age
## kelly stanley cora ## 25 55 60
10.
Use “indexing by number” to get R to print out the first element of one of the vectors you created in the last questions.
age[
1
]
## kelly ## 25
11.
Use logical indexing to return all the ages of all people in age greater than 20.
age
>
20
## kelly stanley cora ## TRUE TRUE TRUE
12.
Use indexing by name to return the age of one of the people whose ages you’ve stored in age
age[
"kelly"
]
## kelly ## 25
Matrices:
Dataframes:
13.
Load the airquality dataset.
14.
Use the $ method to print out the Wind variable in airquality.
15.
Print out the third element of the Wind variable.
airquality
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
## 7 23 299 8.6 65 5 7
## 8 19 99 13.8 59 5 8
## 9 8 19 20.1 61 5 9
## 10 NA 194 8.6 69 5 10
## 11 7 NA 6.9 74 5 11
## 12 16 256 9.7 69 5 12
## 13 11 290 9.2 66 5 13
## 14 14 274 10.9 68 5 14
## 15 18 65 13.2 58 5 15
## 16 14 334 11.5 64 5 16
## 17 34 307 12.0 66 5 17
## 18 6 78 18.4 57 5 18
## 19 30 322 11.5 68 5 19
## 20 11 44 9.7 62 5 20
## 21 1 8 9.7 59 5 21
## 22 11 320 16.6 73 5 22
## 23 4 25 9.7 61 5 23
## 24 32 92 12.0 61 5 24
## 25 NA 66 16.6 57 5 25
## 26 NA 266 14.9 58 5 26
## 27 NA NA 8.0 57 5 27
## 28 23 13 12.0 67 5 28
## 29 45 252 14.9 81 5 29
## 30 115 223 5.7 79 5 30
## 31 37 279 7.4 76 5 31
## 32 NA 286 8.6 78 6 1
## 33 NA 287 9.7 74 6 2
## 34 NA 242 16.1 67 6 3
## 35 NA 186 9.2 84 6 4
## 36 NA 220 8.6 85 6 5
## 37 NA 264 14.3 79 6 6
## 38 29 127 9.7 82 6 7
## 39 NA 273 6.9 87 6 8
## 40 71 291 13.8 90 6 9
## 41 39 323 11.5 87 6 10
## 42 NA 259 10.9 93 6 11
## 43 NA 250 9.2 92 6 12
## 44 23 148 8.0 82 6 13
## 45 NA 332 13.8 80 6 14
## 46 NA 322 11.5 79 6 15
## 47 21 191 14.9 77 6 16
## 48 37 284 20.7 72 6 17
## 49 20 37 9.2 65 6 18
## 50 12 120 11.5 73 6 19
## 51 13 137 10.3 76 6 20
## 52 NA 150 6.3 77 6 21
## 53 NA 59 1.7 76 6 22
## 54 NA 91 4.6 76 6 23
## 55 NA 250 6.3 76 6 24
## 56 NA 135 8.0 75 6 25
## 57 NA 127 8.0 78 6 26
## 58 NA 47 10.3 73 6 27
## 59 NA 98 11.5 80 6 28
## 60 NA 31 14.9 77 6 29
## 61 NA 138 8.0 83 6 30
## 62 135 269 4.1 84 7 1
## 63 49 248 9.2 85 7 2
## 64 32 236 9.2 81 7 3
## 65 NA 101 10.9 84 7 4
## 66 64 175 4.6 83 7 5
## 67 40 314 10.9 83 7 6
## 68 77 276 5.1 88 7 7
## 69 97 267 6.3 92 7 8
## 70 97 272 5.7 92 7 9
## 71 85 175 7.4 89 7 10
## 72 NA 139 8.6 82 7 11
## 73 10 264 14.3 73 7 12
## 74 27 175 14.9 81 7 13
## 75 NA 291 14.9 91 7 14
## 76 7 48 14.3 80 7 15
## 77 48 260 6.9 81 7 16
## 78 35 274 10.3 82 7 17
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help