There are different commands you can use to access data in your R objects, and their use differs slightly between object classes.
# To access an item in a list, just indicate its place within the vector through [ ].
character_vector <- c("Days", "Months", "Year")
character_vector[2]
## [1] "Months"
# If you are unsure about the location of given information in a list, use the which() function.
which(character_vector == "Months")
##[1] 2
Most of the time we work with dataframes. Dataframes have two dimensions – rows and columns. When using [ ] to access values in dataframes, R has an in-built ordering of placing rows before columns i.e. dataframe[row,columm].
# So when you want to access data on "Arkansas" (4th row) on "Murder" (1st column), you would write:
USArrests[4,1]
## [1] 8.8
# If you are interested in all columns for Arkansas just leave the space behind the comma blank.
USArrests[4,]
##
| State | Murder | Assault | Urban Population | Rape |
|---|---|---|---|---|
| Arkansas | 8.8 | 190 | 50 | 19.5 |
# Similarly, if you are interested in murder rates for all states just leave the space in front of the comma blank.
USArrests[,1]
## [1] 13.2 10.0 8.1 8.8 9.0 7.9 3.3 5.9 15.4 17.4 5.3 2.6 10.4 7.2 2.2 6.0 9.7 15.4 2.1 11.3 4.4 12.1 2.7 16.1 9.0 6.0
There are alternative ways to get to the same information.
# To access only the murder rates in the USArrests dataframe, you can call the column name:
USArrests["Murder"]
##
| State | Murder |
|---|---|
| Alabama | 13.2 |
| Alaska | 10.0 |
| Arizona | 8.1 |
| Arkansas | 8.8 |
| California | 9.0 |
| Colorado | 7.9 |
| Connecticut | 3.3 |
| Delaware | 5.9 |
| Florida | 15.4 |
| Georgia | 17.4 |
| Hawaii | 5.3 |
| Idaho | 2.6 |
| Illinois | 10.4 |
| Indiana | 7.2 |
| Iowa | 2.2 |
| Kansas | 6.0 |
| Kentucky | 9.7 |
| Louisiana | 15.4 |
| Maine | 2.1 |
| Maryland | 11.3 |
| Massachusetts | 4.4 |
| Michigan | 12.1 |
| Minnesota | 2.7 |
| Mississippi | 16.1 |
| Missouri | 9.0 |
| Montana | 6.0 |
| Nebraska | 4.3 |
| Nevada | 12.2 |
| New Hampshire | 2.1 |
| New Jersey | 7.4 |
| New Mexico | 11.4 |
| New York | 11.1 |
| North Carolina | 13.0 |
| North Dakota | 0.8 |
| Ohio | 7.3 |
| Oklahoma | 6.6 |
| Oregan | 4.9 |
| Pennsylvania | 6.3 |
| Rhode Island | 3.4 |
| South Carolina | 14.4 |
| South Dakota | 3.8 |
| Tennessee | 13.2 |
| Texas | 12.7 |
| Utah | 3.2 |
| Vermont | 2.2 |
| Virginia | 8.5 |
| Washington | 4.0 |
| West Virginia | 5.7 |
| Wisconsin | 2.6 |
| Wyoming | 6.8 |
# Alternatively, to access the data in a column in a dataframe we can also use the $ character:
USArrests$Murder
## [1] 13.2 10.0 8.1 8.8 9.0 7.9 3.3 5.9 15.4 17.4 5.3 2.6 10.4 7.2 2.2 6.0 9.7 15.4 2.1 11.3 4.4 12.1 2.7 16.1 9.0 6.0
You can combine the above $ and [ ] approaches.
# For instance, to again get the murder rates in Arkansas, you can write:
USArrests$Murder[4]
## [1] 8.8
Sometimes we are only interested in subsets of the data, we then create objects from larger dataframes.
# For instance, we want to create a new dataframe that only has the murder rate data.
murder_rate <- USArrests["Murder"]
# Or we want to exclude murder rates from our dataset. Then we can simply write [,-1] which will eliminate the first column.
other_crimes <- USArrests[,-1]
print(other_crimes)
##
| State | Assault | Urban Population | Rape |
|---|---|---|---|
| Alabama | 236 | 58 | 21.2 |
| Alaska | 263 | 48 | 44.5 |
| Arizona | 294 | 80 | 31.0 |
| Arkansas | 190 | 50 | 19.5 |
| California | 276 | 91 | 40.6 |
| Colorado | 204 | 78 | 38.7 |
| Connecticut | 110 | 77 | 11.1 |
| Delaware | 238 | 72 | 15.8 |
| Florida | 335 | 80 | 31.9 |
| Georgia | 211 | 60 | 25.8 |
| Hawaii | 46 | 83 | 20.2 |
| Idaho | 120 | 54 | 14.2 |
| Illinois | 249 | 83 | 24.0 |
| Indiana | 113 | 65 | 21.0 |
| Iowa | 56 | 57 | 11.3 |
| Kansas | 115 | 66 | 18.0 |
| Kentucky | 109 | 52 | 16.3 |
| Louisiana | 249 | 66 | 22.2 |
| Maine | 83 | 51 | 7.8 |
| Maryland | 300 | 67 | 27.8 |
| Massachusetts | 149 | 85 | 16.3 |
| Michigan | 255 | 74 | 35.1 |
| Minnesota | 72 | 66 | 14.9 |
| Mississippi | 259 | 44 | 17.1 |
| Missouri | 178 | 70 | 28.2 |
| Montana | 109 | 53 | 16.4 |
| Nebraska | 102 | 62 | 16.5 |
| Nevada | 252 | 81 | 46.0 |
| New Hampshire | 57 | 56 | 9.5 |
| New Jersey | 159 | 89 | 18.8 |
| New Mexico | 285 | 70 | 32.1 |
| New York | 254 | 86 | 26.1 |
| North Carolina | 337 | 45 | 16.1 |
| North Dakota | 45 | 44 | 7.3 |
| Ohio | 120 | 75 | 21.4 |
| Oklahoma | 151 | 68 | 20.0 |
| Oregan | 159 | 67 | 29.3 |
| Pennsylvania | 106 | 72 | 14.9 |
| Rhode Island | 174 | 87 | 8.3 |
| South Carolina | 279 | 48 | 22.5 |
| South Dakota | 86 | 45 | 12.8 |
| Tennessee | 188 | 59 | 26.9 |
| Texas | 201 | 80 | 25.5 |
| Utah | 120 | 80 | 22.9 |
| Vermont | 48 | 32 | 11.2 |
| Virginia | 156 | 63 | 20.7 |
| Washington | 145 | 73 | 26.2 |
| West Virginia | 81 | 39 | 9.3 |
| Wisconsin | 53 | 66 | 10.8 |
| Wyoming | 161 | 60 | 15.6 |
In order to combine datasets, we use two functions. The function rbind() glues two datasets together by their rows, while cbind() connects them by their columns.
# So if we want to merge the two datasets again, we can do:
full_dataset <- cbind(murder_rate,other_crimes)
print(full_dataset)
##
| State | Murder | Assault | Urban Population | Rape |
|---|---|---|---|---|
| Alabama | 13.2 | 236 | 58 | 21.2 |
| Alaska | 10.0 | 263 | 48 | 44.5 |
| Arizona | 8.1 | 294 | 80 | 31.0 |
| Arkansas | 8.8 | 190 | 50 | 19.5 |
| California | 9.0 | 276 | 91 | 40.6 |
| Colorado | 7.9 | 204 | 78 | 38.7 |
| Connecticut | 3.3 | 110 | 77 | 11.1 |
| Delaware | 5.9 | 238 | 72 | 15.8 |
| Florida | 15.4 | 335 | 80 | 31.9 |
| Georgia | 17.4 | 211 | 60 | 25.8 |
| Hawaii | 5.3 | 46 | 83 | 20.2 |
| Idaho | 2.6 | 120 | 54 | 14.2 |
| Illinois | 10.4 | 249 | 83 | 24.0 |
| Indiana | 7.2 | 113 | 65 | 21.0 |
| Iowa | 2.2 | 56 | 57 | 11.3 |
| Kansas | 6.0 | 115 | 66 | 18.0 |
| Kentucky | 9.7 | 109 | 52 | 16.3 |
| Louisiana | 15.4 | 249 | 66 | 22.2 |
| Maine | 2.1 | 83 | 51 | 7.8 |
| Maryland | 11.3 | 300 | 67 | 27.8 |
| Massachusetts | 4.4 | 149 | 85 | 16.3 |
| Michigan | 12.1 | 255 | 74 | 35.1 |
| Minnesota | 2.7 | 72 | 66 | 14.9 |
| Mississippi | 16.1 | 259 | 44 | 17.1 |
| Missouri | 9.0 | 178 | 70 | 28.2 |
| Montana | 6.0 | 109 | 53 | 16.4 |
| Nebraska | 4.3 | 102 | 62 | 16.5 |
| Nevada | 12.2 | 252 | 81 | 46.0 |
| New Hampshire | 2.1 | 57 | 56 | 9.5 |
| New Jersey | 7.4 | 159 | 89 | 18.8 |
| New Mexico | 11.4 | 285 | 70 | 32.1 |
| New York | 11.1 | 254 | 86 | 26.1 |
| North Carolina | 13.0 | 337 | 45 | 16.1 |
| North Dakota | 0.8 | 45 | 44 | 7.3 |
| Ohio | 7.3 | 120 | 75 | 21.4 |
| Oklahoma | 6.6 | 151 | 68 | 20.0 |
| Oregan | 4.9 | 159 | 67 | 29.3 |
| Pennsylvania | 6.3 | 106 | 72 | 14.9 |
| Rhode Island | 3.4 | 174 | 87 | 8.3 |
| South Carolina | 14.4 | 279 | 48 | 22.5 |
| South Dakota | 3.8 | 86 | 45 | 12.8 |
| Tennessee | 13.2 | 188 | 59 | 26.9 |
| Texas | 12.7 | 201 | 80 | 25.5 |
| Utah | 3.2 | 120 | 80 | 22.9 |
| Vermont | 2.2 | 48 | 32 | 11.2 |
| Virginia | 8.5 | 156 | 63 | 20.7 |
| Washington | 4.0 | 145 | 73 | 26.2 |
| West Virginia | 5.7 | 81 | 39 | 9.3 |
| Wisconsin | 2.6 | 53 | 66 | 10.8 |
| Wyoming | 6.8 | 161 | 60 | 15.6 |
access_time Last update January 7, 2021.