Lab 01 R Learning ✓ Solved

Lab 01 R Learning

Lab 01 R Learning

1. Use logical operations to get R to agree that "two plus two equals 5" is FALSE.

2. Use logical operations to test whether 8 ^ 13 is less than 15 ^ 9.

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.

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.

5. Reassign the value of potato to potato * 2. Print out the new value of potato to verify that it has changed.

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.

7. Create a numeric vector with three elements using c().

8. Create a character vector with three elements using 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.

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.

11. Use logical indexing to return all the ages of all people in age greater than 20.

12. Use indexing by name to return the age of one of the people whose ages you've stored in age.

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.

16. Create a new data frame called aq that includes only the first 10 cases.

17. Use logical indexing to print out all days (i.e., cases) in aq where the Ozone level was higher than 20.

18. Use subset() to do the same thing.

19. Create a TooWindy variable inside aq, which is a logical variable that is TRUE if Wind is greater than 10, and FALSE otherwise.

20. Use the length() function to determine the number of observations in the airquality dataframe.

21. Calculate the mean and standard deviation of one of the variables in airquality.

22. Make a table of the Temp values.

23. Make a histogram of the Ozone column. Is it a normal distribution? Why or why not?

24. Make a simple function to calculate x + 6.

25. Use that function to add 6 to the Temp column in airquality.

26. Install the ggplot2 package.

27. Install the car package.

28. Install the ez package.

29. Load the car library.

30. Import the csv file provided on Canvas.

Paper For Above Instructions

This lab focuses on the practical application of R programming through the use of logical operators, variables, vectors, matrices, data frames, functions, and packages. In R, logical operators are essential for making comparisons and evaluations. For instance, to verify the statement "two plus two equals 5," one could use the following code:

result 

The output of 'result' will be FALSE, affirming our logical evaluation.

Another important logical operation could be comparing two large powers, such as testing if 8 ^ 13 is less than 15 ^ 9.

comparison 

This comparison allows us to understand how R handles very large numbers, reinforced by R's ability to store and manipulate them efficiently.

Next, we create a variable called 'potato' representing the number of potatoes I have eaten in the past week. For example, if I have eaten three potatoes, we can assign and print this variable:

potato 

print(potato)

To illustrate the point of variable immutability, we can compute the square root of 'potato'. Importantly, this should not change the original value of 'potato':

sqrt_potato 

print(potato) # Should still return 3

When it comes to variable reassignment, we can double the number of potatoes eaten. The method should look like this:

potato 

print(potato) # Should now return 6

Creating variables of various types—specifically, a character variable and a logical variable—can easily be achieved:

character_var 

logical_var

na_var

It's also vital to ensure that we correctly classify our variables using the class function:

class(character_var)

class(logical_var)

class(na_var)

Vectors are fundamental in R for grouping data. A numeric vector can be constructed with

numeric_vector 

A character vector follows a similar structure:

char_vector 

For demographic representation, we could create a named age vector:

age 

Indexing allows us to interact with these data structures efficiently. For example, printing the first element can be achieved via:

age[1]

We can also filter using logical conditions:

age[age > 20]

For matrices and data frames, practical data manipulation can be accomplished by loading datasets such as 'airquality', which includes weather data.

data(airquality)

The example below demonstrates how to access specific columns and rows:

airquality$Wind # To access Wind variable

airquality$Wind[3] # To print the third Wind value

Working with a subset of dataframes is also straight forward:

aq 

Logical indexing in conjunction with data frames allows us to query specific conditions such as:

aq[aq$Ozone > 20, ]

We also create new logical variables:

aq$TooWindy  10

Statistical functions such as mean and standard deviation can be applied:

mean(aq$Temp)

sd(aq$Temp)

To visualize data distributions, histograms are useful, while functions can be defined easily to enhance reusability:

my_function 

new_temps

In addition, managing R packages is essential. Installing package examples: ggplot2, car, and ez can be done using:

install.packages("ggplot2")

install.packages("car")

install.packages("ez")

References

  • R Core Team. (2023). R: A language and environment for statistical computing.
  • Wickham, H. (2016). ggplot2: Elegant Graphics for Data Analysis. Springer.
  • RStudio Team. (2020). RStudio: Integrated Development Environment for R.
  • Becker, R. A., Chambers, J. M., & Wilks, A. R. (1988). The New S Language. Wadsworth & Brooks/Cole.
  • Dalgaard, P. (2008). Introductory Statistics with R. Springer.
  • Manning, W. (2018). R in Action: Data Analysis and Graphics with R.
  • Hyndman, R. J., & Athanasopoulos, G. (2021). Forecasting: Principles and Practice.
  • Mihailovic, A. (2023). R and Data Mining: Examples and Case Studies.
  • Harris, R. (2022). R for Data Science: Import, Tidy, Transform, Visualize, and Model Data.
  • R Documentation. (2023). Retrieved from https://www.rdocumentation.org/