Suppose a high school student asks us for help solving several
quadratic equations of the form \(ax^2+bx+c=0\). The quadratic formula gives
us the solutions: \(\frac{-b\pm\sqrt(b^2-4ac))}{2a}\), which of
course change depending on the values of
a, b, and c. We will write out general code for the quadratic equation
below, but if we are asked to solve \(x^2+x-1=0\), then we define:
a <- 1
b <- 1
c <- -1
which stores the values for later use. We use <- to assign values to the variables in R. Although = also works for R, we recommend against using =.
a
## [1] 1
b
## [1] 1
c
## [1] -1
c(a,b,c)
## [1] 1 1 -1
c("a","b","c")
## [1] "a" "b" "c"
In RStudio, the Environment tab shows the values: We should see a, b, and c. If you try to recover the value of a variable that is not in your workspace, you receive an error. For example, if you type x you will receive the following message \(\texttt{Error: object 'x' not found}\).
Now since these values are saved in variables, to obtain a solution to our equation, we use the quadratic formula:
s1 <- (-b + sqrt(b^2 - 4*a*c))/(2*a)
s2 <- (-b - sqrt(b^2 - 4*a*c))/(2*a)
round(s1,3)
## [1] 0.618
round(s2,3)
## [1] -1.618
R provides a wide range of predefined functions that are commonly used for various tasks, from data manipulation to statistical analysis. Below is a list of some of the top predefined functions in R, categorized by their general use case. We already used \(\texttt{install.pcackages}, \texttt{library}, \texttt{c}\) and \(\texttt{plot}\) functions.
log(8)
## [1] 2.079442
log(a)
## [1] 0
You can find out what the function expects and what it does by reviewing the very useful manuals included in R. You can get help by using the help function like this:
help("log")
For most functions, we can also use this shorthand:
?log
You can change the base values by simply assigning another object:
log(8,2)
## [1] 3
#There are several datasets that are included for users to practice and test out functions. You can see all the available datasets by typing:
data()
#This shows you the object name for these datasets. These datasets are objects that can be used by simply typing the name. For example, if you type:
co2
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct
## 1959 315.42 316.31 316.50 317.56 318.13 318.00 316.39 314.65 313.68 313.18
## 1960 316.27 316.81 317.42 318.87 319.87 319.43 318.01 315.74 314.00 313.68
## 1961 316.73 317.54 318.38 319.31 320.42 319.61 318.42 316.63 314.83 315.16
## 1962 317.78 318.40 319.53 320.42 320.85 320.45 319.45 317.25 316.11 315.27
## 1963 318.58 318.92 319.70 321.22 322.08 321.31 319.58 317.61 316.05 315.83
## 1964 319.41 320.07 320.74 321.40 322.06 321.73 320.27 318.54 316.54 316.71
## 1965 319.27 320.28 320.73 321.97 322.00 321.71 321.05 318.71 317.66 317.14
## 1966 320.46 321.43 322.23 323.54 323.91 323.59 322.24 320.20 318.48 317.94
## 1967 322.17 322.34 322.88 324.25 324.83 323.93 322.38 320.76 319.10 319.24
## 1968 322.40 322.99 323.73 324.86 325.40 325.20 323.98 321.95 320.18 320.09
## 1969 323.83 324.26 325.47 326.50 327.21 326.54 325.72 323.50 322.22 321.62
## 1970 324.89 325.82 326.77 327.97 327.91 327.50 326.18 324.53 322.93 322.90
## 1971 326.01 326.51 327.01 327.62 328.76 328.40 327.20 325.27 323.20 323.40
## 1972 326.60 327.47 327.58 329.56 329.90 328.92 327.88 326.16 324.68 325.04
## 1973 328.37 329.40 330.14 331.33 332.31 331.90 330.70 329.15 327.35 327.02
## 1974 329.18 330.55 331.32 332.48 332.92 332.08 331.01 329.23 327.27 327.21
## 1975 330.23 331.25 331.87 333.14 333.80 333.43 331.73 329.90 328.40 328.17
## 1976 331.58 332.39 333.33 334.41 334.71 334.17 332.89 330.77 329.14 328.78
## 1977 332.75 333.24 334.53 335.90 336.57 336.10 334.76 332.59 331.42 330.98
## 1978 334.80 335.22 336.47 337.59 337.84 337.72 336.37 334.51 332.60 332.38
## 1979 336.05 336.59 337.79 338.71 339.30 339.12 337.56 335.92 333.75 333.70
## 1980 337.84 338.19 339.91 340.60 341.29 341.00 339.39 337.43 335.72 335.84
## 1981 339.06 340.30 341.21 342.33 342.74 342.08 340.32 338.26 336.52 336.68
## 1982 340.57 341.44 342.53 343.39 343.96 343.18 341.88 339.65 337.81 337.69
## 1983 341.20 342.35 342.93 344.77 345.58 345.14 343.81 342.21 339.69 339.82
## 1984 343.52 344.33 345.11 346.88 347.25 346.62 345.22 343.11 340.90 341.18
## 1985 344.79 345.82 347.25 348.17 348.74 348.07 346.38 344.51 342.92 342.62
## 1986 346.11 346.78 347.68 349.37 350.03 349.37 347.76 345.73 344.68 343.99
## 1987 347.84 348.29 349.23 350.80 351.66 351.07 349.33 347.92 346.27 346.18
## 1988 350.25 351.54 352.05 353.41 354.04 353.62 352.22 350.27 348.55 348.72
## 1989 352.60 352.92 353.53 355.26 355.52 354.97 353.75 351.52 349.64 349.83
## 1990 353.50 354.55 355.23 356.04 357.00 356.07 354.67 352.76 350.82 351.04
## 1991 354.59 355.63 357.03 358.48 359.22 358.12 356.06 353.92 352.05 352.11
## 1992 355.88 356.63 357.72 359.07 359.58 359.17 356.94 354.92 352.94 353.23
## 1993 356.63 357.10 358.32 359.41 360.23 359.55 357.53 355.48 353.67 353.95
## 1994 358.34 358.89 359.95 361.25 361.67 360.94 359.55 357.49 355.84 356.00
## 1995 359.98 361.03 361.66 363.48 363.82 363.30 361.94 359.50 358.11 357.80
## 1996 362.09 363.29 364.06 364.76 365.45 365.01 363.70 361.54 359.51 359.65
## 1997 363.23 364.06 364.61 366.40 366.84 365.68 364.52 362.57 360.24 360.83
## Nov Dec
## 1959 314.66 315.43
## 1960 314.84 316.03
## 1961 315.94 316.85
## 1962 316.53 317.53
## 1963 316.91 318.20
## 1964 317.53 318.55
## 1965 318.70 319.25
## 1966 319.63 320.87
## 1967 320.56 321.80
## 1968 321.16 322.74
## 1969 322.69 323.95
## 1970 323.85 324.96
## 1971 324.63 325.85
## 1972 326.34 327.39
## 1973 327.99 328.48
## 1974 328.29 329.41
## 1975 329.32 330.59
## 1976 330.14 331.52
## 1977 332.24 333.68
## 1978 333.75 334.78
## 1979 335.12 336.56
## 1980 336.93 338.04
## 1981 338.19 339.44
## 1982 339.09 340.32
## 1983 340.98 342.82
## 1984 342.80 344.04
## 1985 344.06 345.38
## 1986 345.48 346.72
## 1987 347.64 348.78
## 1988 349.91 351.18
## 1989 351.14 352.37
## 1990 352.69 354.07
## 1991 353.64 354.89
## 1992 354.09 355.33
## 1993 355.30 356.78
## 1994 357.59 359.05
## 1995 359.61 360.74
## 1996 360.80 362.38
## 1997 362.49 364.34
#R will show you Mauna Loa atmospheric CO2 concentration data.
Other prebuilt objects are mathematical quantities, such as the constant
pi
## [1] 3.141593
Inf
## [1] Inf
log()
: Computes logarithms (natural by
default, can specify base).exp()
: Computes the exponential of a
number.sqrt()
: Computes the square root of a
number.abs()
: Computes the absolute value of
a number.round()
: Rounds a number to a
specified number of decimal places.ceiling()
: Rounds a number up to the
nearest integer.floor()
: Rounds a number down to the
nearest integer.cos()
,
sin()
,
tan()
: Trigonometric functions.log10()
: Computes the base-10
logarithm.head()
: Displays the first few rows of
a dataset.tail()
: Displays the last few rows of
a dataset.str()
: Displays the structure of an
object, including data types and a preview of the data.summary()
: Provides summary statistics
for each variable in a dataset.names()
: Lists the column names of a
data frame.dim()
: Returns the dimensions (number
of rows and columns) of a matrix or data frame.length()
: Returns the length of a
vector or the number of elements in a list.subset()
: Extracts subsets of data
based on conditions.merge()
: Merges two data frames by
common columns or row names.rbind()
: Combines data frames or
matrices by rows.cbind()
: Combines data frames or
matrices by columns.apply()
: Applies a function to rows or
columns of a matrix or data frame.lapply()
: Applies a function to each
element of a list.sapply()
: Similar to
lapply()
, but tries to simplify the result.mean()
: Calculates the arithmetic mean
of a numeric vector.median()
: Calculates the median of a
numeric vector.sd()
: Calculates the standard
deviation of a numeric vector.var()
: Calculates the variance of a
numeric vector.sum()
: Returns the sum of all elements
in a numeric vector.min()
: Returns the minimum value in a
numeric vector.max()
: Returns the maximum value in a
numeric vector.quantile()
: Calculates the quantiles
of a numeric vector.cor()
: Computes the correlation
between two numeric vectors.lm()
: Fits a linear model to
data.transform()
: Adds or modifies columns
in a data frame.aggregate()
: Splits data into subsets,
computes summary statistics, and returns the results.t()
: Transposes a matrix or data
frame.rep()
: Replicates the elements of
vectors.seq()
: Generates sequences of
numbers.plot()
: Creates a scatter plot or
other types of plots depending on the data type.hist()
: Plots a histogram.boxplot()
: Creates a box plot.barplot()
: Creates a bar plot.pairs()
: Creates a matrix of
scatterplots.ifelse()
: Vectorized conditional
operation.which()
: Identifies the indices of
elements that satisfy a given condition.match()
: Returns the positions of
(first) matches of its first argument in its second.unique()
: Returns a vector, data
frame, or array with duplicate elements removed.sort()
: Sorts a vector or factor.order()
: Returns the indices of the
sorted elements of a vector.print()
: Prints an object.cat()
: Concatenates and prints
objects.paste()
: Concatenates vectors after
converting to character.system.time()
: Reports the time taken
to evaluate an expression.gc()
: Runs garbage collection,
cleaning up memory.