3 min read•june 18, 2024
R is a powerful tool for statistical analysis, offering versatile functions for data manipulation and visualization. It allows users to create vectors, perform basic statistical operations, and generate informative plots with ease.
R's data structures, like data frames and matrices, enable efficient organization of complex datasets. Custom functions and packages expand R's capabilities, making it a flexible platform for various statistical tasks and data science projects.
[c()](https://www.fiveableKeyTerm:c())
to combine elements
x <- c(1, 2, 3, 4, 5)
creates a numeric with values 1, 2, 3, 4, and 5y <- c("a", "b", "c")
creates a character vector with values "a", "b", and "c":
operator
z <- 1:10
creates a numeric vector with values 1, 2, 3, ..., 10seq(from = 0, to = 1, by = 0.1)
creates a sequence from 0 to 1 in increments of 0.1<-
prices <- c(10.99, 15.50, 8.75)
assigns the vector to the variable prices
length(x)
returns the number of elements in vector x
sum(x)
calculates the sum of all elements in numeric vector x
mean(x)
calculates the arithmetic mean of numeric vector x
median(x)
calculates the median value of numeric vector x
min(x)
and max(x)
return the minimum and maximum values in numeric vector x
var(x)
and sd(x)
calculate the variance and standard deviation of numeric vector x
summary(x)
provides a summary of the distribution of vector x
, including minimum, maximum, median, and quartile valuesx + y
adds corresponding elements of vectors x
and y
x * 2
multiplies each element of vector x
by 2x[x > 3]
returns a vector containing only the elements of x
greater than 3y[y == "a"]
returns a vector containing only the elements of y
equal to "a"[plot()](https://www.fiveableKeyTerm:plot())
function
plot(x, y)
creates a scatter plot with x
values on the x-axis and y
values on the y-axismain
, xlab
, ylab
, col
, pch
[hist()](https://www.fiveableKeyTerm:hist())
function
hist(x)
creates a histogram of the values in vector x
breaks
, col
, main
[boxplot()](https://www.fiveableKeyTerm:boxplot())
function
boxplot(x)
creates a box plot of the values in vector x
boxplot(x ~ f)
creates box plots of x
grouped by the levels of factor f
[par()](https://www.fiveableKeyTerm:par())
function with mfrow
or mfcol
argument
par(mfrow = c(2, 2))
creates a 2x2 grid of plotsdata.frame()
function$
operator or by name with square brackets[matrix](https://www.fiveableKeyTerm:Matrix)()
function or by combining vectorsfactor()
functionfunction()
keywordinstall.packages()
and load them with library()