R_Intro

What is R and R Studio?

R was developed by statisticians and data analysts. It is free and open source, has capability to save scripts, and many resources for learning, and easy for developers to share software implementations.

We can us install package scripts and library function.

# installing the dslabs package
install.packages("dslabs")
# loading the dslabs package into the R session
library(dslabs)
# to install two packages at the same time
install.packages(c("tidyverse", "dslabs")) 

Objects in R

a <- 1
b <- 1
c <- -1
# solving the quadratic equation
(-b + sqrt(b^2 - 4*a*c))/(2*a)

Data Types

# determining that the murders dataset is of the "data frame" class
class(murders)
# finding out more about the structure of the object
str(murders)
# showing the first 6 lines of the dataset
head(murders)

# using the accessor operator to obtain the population column
murders$population
# displaying the variable names in the murders dataset
names(murders)
# determining how many entries are in a vector
pop <- murders$population
length(pop)
# vectors can be of class numeric and character
class(pop)
class(murders$state)

# factors are another type of class
class(murders$region)
# obtaining the levels of a factor
levels(murders$region)

# creating Dataframes
states <- murders$state
ranks <- rank(murders$population)
my_df <- data.frame(name = states, rank = ranks)