01_lab

.docx

School

Harrisburg University of Science and Technology *

*We aren’t endorsed by this school

Course

500

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

6

Uploaded by DrSparrow3631 on coursehero.com

Lab 01 R Learning 2022-09-07 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 <- 2 potato ## [1] 2 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] 1.414214 potato ## [1] 2 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] 4 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(variable name) to make sure they are the right type of variable.
a <- "go!" b <- T c <- NA class (a) ## [1] "character" class (b) ## [1] "logical" class (c) ## [1] "logical" Vectors: 7. Create a numeric vector with three elements using c(). num <- c ( 1 , 2 , 4 ) 8. Create a character vector with three elements using c(). chr <- c ( "A" , "B" , "C" ) 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 ( 20 , 25 , 52 ) names (age) <- c ( "Rose" , "Regina" , "Holly" ) 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. num[ 1 ] ## [1] 1 11. Use logical indexing to return all the ages of all people in age greater than 20. age[age > 20 ] ## Regina Holly ## 25 52 12. Use indexing by name to return the age of one of the people whose ages you’ve stored in age age[ c ( "Rose" )] ## Rose ## 20
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. View (airquality) airquality $ Wind ## [1] 7.4 8.0 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 6.9 9.7 9.2 10.9 13.2 ## [16] 11.5 12.0 18.4 11.5 9.7 9.7 16.6 9.7 12.0 16.6 14.9 8.0 12.0 14.9 5.7 ## [31] 7.4 8.6 9.7 16.1 9.2 8.6 14.3 9.7 6.9 13.8 11.5 10.9 9.2 8.0 13.8 ## [46] 11.5 14.9 20.7 9.2 11.5 10.3 6.3 1.7 4.6 6.3 8.0 8.0 10.3 11.5 14.9 ## [61] 8.0 4.1 9.2 9.2 10.9 4.6 10.9 5.1 6.3 5.7 7.4 8.6 14.3 14.9 14.9 ## [76] 14.3 6.9 10.3 6.3 5.1 11.5 6.9 9.7 11.5 8.6 8.0 8.6 12.0 7.4 7.4 ## [91] 7.4 9.2 6.9 13.8 7.4 6.9 7.4 4.6 4.0 10.3 8.0 8.6 11.5 11.5 11.5 ## [106] 9.7 11.5 10.3 6.3 7.4 10.9 10.3 15.5 14.3 12.6 9.7 3.4 8.0 5.7 9.7 ## [121] 2.3 6.3 6.3 6.9 5.1 2.8 4.6 7.4 15.5 10.9 10.3 10.9 9.7 14.9 15.5 ## [136] 6.3 10.9 11.5 6.9 13.8 10.3 10.3 8.0 12.6 9.2 10.3 10.3 16.6 6.9 13.2 ## [151] 14.3 8.0 11.5 airquality $ Wind[ 3 ] ## [1] 12.6 16. Create a new data frame called aq that includes only the first 10 cases. Hint: typing c(1,2,3,4,5,6,7,8,9,10) is tedious. R allows you to use 1:10 as a shorthand method! 17. Use logical indexing to print out all days (ie. cases) in aq where the Ozone level was higher than 20. a. What did the output do with NA values? 18. Use subset() to do the same thing. Notice the difference in the output. aq <- airquality[ 1 : 10 ,] aq[aq $ Ozone > 20 ,]
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