In R, there are different ways for data to be formatted that will all have different outcomes of how the data is treated. This markdown file aims to provide some examples of different classes of values and functions and how they can be used in R.

Objective for this assignment is to create examples of the following commands in R

datetime character numeric boolean array vector data frame list tibble

library("lubridate")
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library("tibble")

1: datetime function The datetime function has many uses and subfunctions all relating to how we present data involving time. For this example I will use subfunctions. The today function will spit out what the date is in a yy;mm;dd format. I can use different datetime commands to change the output to show me just the year, month, or day of the month depending on what I want.

Today<-format(today())
Year<-format(today(), "%Y")
Month<-format(today(), "%m")
Day<-format(today(), "%d")
str(Today)
##  chr "2024-09-30"
str(Year)
##  chr "2024"
str(Month)
##  chr "09"
str(Day)
##  chr "30"

2: Character Below, I am creating a vector called “CourseNames” and telling R the course codes of four classes I have taken so far. This is just the way R will hold this data. When I use str to check the type of data we can see it returns as a character, because I am using quotation marks to list the names of courses.

CourseNames<- c("BIN510","BIN501","BIN521", "BIN520")
str(CourseNames)
##  chr [1:4] "BIN510" "BIN501" "BIN521" "BIN520"

3: Numeric Going off of what we did for charcacter, I can make a vector with grades I recieved in my courses. Because we want these values to be numeric, I will use the round number of my grade and not use quotation marks and R will see this as a numeric vector.

CourseGrades<- c(96.45,88.57,98.0,93.51)
str(CourseGrades)
##  num [1:4] 96.5 88.6 98 93.5

4: Boolean A Boolean value is a type of data that has one of two values, true or false. This function can be used for conditional testing. Let’s see if we can use a boolean function to return a response about grades in my classes thus far. I will use a boolean function to tell me if i’ve passed my classes (true) or failed (false) by setting a passing grade as 70. I will rerun this function with new labels for an outcome for my higher grades

Pass<- 70
outcome<- for (i in 1:length(CourseGrades)) {
  if (CourseGrades[i]>Pass){
    print("Passed")
  } else {
  print("Failed")
  }}
## [1] "Passed"
## [1] "Passed"
## [1] "Passed"
## [1] "Passed"

For comparison sake, I will now use the same boolean command but instead of a passing grade, I will have R detect the courses I did “amazing” in vs courses that I “did great” in based on a value of 95 or higher

High<- 95
outcome<- for (i in 1:length(CourseGrades)) {
  if (CourseGrades[i]>High){
    print("Amazing")
  } else {
  print("Did Great")
  }}
## [1] "Amazing"
## [1] "Did Great"
## [1] "Amazing"
## [1] "Did Great"

5: Array An array is an arrangement of numbers/values formatted into rows and columns based on data type. Array functions in R will help you create, manage, and manipulate these lists of values and can help you organize efficiently. Let’s combine the vectors of my course name and grades into one location now.

ClassInfo <-(rbind(CourseNames, CourseGrades))
str(ClassInfo)
##  chr [1:2, 1:4] "BIN510" "96.45" "BIN501" "88.57" "BIN521" "98" "BIN520" ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : chr [1:2] "CourseNames" "CourseGrades"
##   ..$ : NULL

6: Vector A vector in R is essentially a list of variables/ is a collection of values that could be logical values, a character, numbers, etc. We have been using vectors this entire time! We made vectors for the datetime examples (Day, Month, Year); for my course names, grades, and even the cuttoff values for if I passed a class or if I got above a 95, have all been vectors we have made in R for these various applications. Here I will just list the names of the vectors we’ve made and you’ll see I don’t need to do any command, R will just tell me what is stored in each vector.

Day
## [1] "30"
Month
## [1] "09"
Year
## [1] "2024"
Pass
## [1] 70
High
## [1] 95
CourseGrades 
## [1] 96.45 88.57 98.00 93.51
CourseNames
## [1] "BIN510" "BIN501" "BIN521" "BIN520"

7: data frame Data frames help you store data. Information stored in the data frame does not all need to be the same kind, but it does all need to be the same length. Let’s turn the array of course names and grades into a dataframe of my course names and grades so that it is all in once place. The output of this looks very similar to the output of our array example, which is true, but they do have differences. An array differs from a data frame because only holds the same type of data and it maintains its two dimensional rectangular organization (fixed number of rows and columns) whereas a dataframe can store as many types and as many columns of data it wants to.

ClassInfodf <-as.data.frame(ClassInfo)
str(ClassInfodf)
## 'data.frame':    2 obs. of  4 variables:
##  $ V1: chr  "BIN510" "96.45"
##  $ V2: chr  "BIN501" "88.57"
##  $ V3: chr  "BIN521" "98"
##  $ V4: chr  "BIN520" "93.51"

8: list We can use R to create lists that contain strings, numbers, vectors, and values. For this example, I can use the list function to create a list of my top 3 ice cream flavors. I will make 3 attributes: 1= order of flavors I like. 2= the names of flavors. 3= the number of flavors I am making in my list. I can then combine attributes to make my list. I can also turn this list into a dataframe to show how they will appear differently in raw list format vs data frame format.

flavororder = c(1,2, 3)
flavorname = c("Purple Cow", "Moose Tracks", "Pumpkin")
numberofflavor=3
IceCreamList = list(flavororder, flavorname, numberofflavor)
print(IceCreamList)
## [[1]]
## [1] 1 2 3
## 
## [[2]]
## [1] "Purple Cow"   "Moose Tracks" "Pumpkin"     
## 
## [[3]]
## [1] 3
IceCreamDF<-as.data.frame(IceCreamList)
IceCreamDF
##   c.1..2..3. c..Purple.Cow....Moose.Tracks....Pumpkin.. X3
## 1          1                                 Purple Cow  3
## 2          2                               Moose Tracks  3
## 3          3                                    Pumpkin  3

9: tibble Tibble is a function that will help to not overwhelm your computer when working with large data sets. It does this by limiting the amount of data it prints, it specifies the type of data of the columns similar to the str function. You can use tibble to make a new data frame following another function. For example I will first make a tibble of my classinfodf and then I will use tibble to give me some statistical data on my grades

Tibbleclassinfo <-as_tibble(ClassInfodf)
str(Tibbleclassinfo)
## tibble [2 × 4] (S3: tbl_df/tbl/data.frame)
##  $ V1: chr [1:2] "BIN510" "96.45"
##  $ V2: chr [1:2] "BIN501" "88.57"
##  $ V3: chr [1:2] "BIN521" "98"
##  $ V4: chr [1:2] "BIN520" "93.51"