R Flashcards
(41 cards)
assign a value to a variable
use
determining that the object (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)
obtain a specific column (ex population)
murders$population
$ is called accessor operator
displaying the variable names in the murders dataset
names(murders)
determining how many entries are in a vector
pop
obtaining the levels of a factor
levels(murders$region)
obtaining the number of levels of a factor
nlevels()
create vectors of class numeric or character
use “concentrate function”
ex: codes
name the elements of a numeric vector
codes
name the elements of a numeric vector in a different way
codes
access specific elements of a vector
Using square brackets
The function ____ sorts a vector in increasing order.
sort()
The function _____ produces the indices needed to obtain the sorted vector
order()
The function ____ gives us the ranks of the items in the original vector
rank()
The function ____ returns the largest/smallest value
max() and min()
The function ____ returns the index of the largest/smallest value
which.max() and which.min()
The name of the state with the maximum population is found by doing ??
murders$state[which.max(murders$population)]
how to obtain the murder rate
murder_rate
ordering the states by murder rate, in decreasing order
murders$state[order(murder_rate, decreasing=TRUE)]
Creating a logical vector that specifies if the murder rate in that state is less than or equal to 0.71
index
Determining which states have murder rates less than or equal to 0.71
murders$state[index]
Calculating how many states have a murder rate less than or equal to 0.71
sum(index)