3.5 Wrangling data frames
Now that you’re able to successfully import your data from an external file into R our next task is to do something useful with our data. Working with data is a fundamental skill which you’ll need to develop and get comfortable with as you’ll likely do a lot of it during any project. The good news is that R is especially good at manipulating, summarising and visualising data. Manipulating data (often known as data wrangling or munging) in R can at first seem a little daunting for the new user but if you follow a few simple logical rules then you’ll quickly get the hang of it, especially with some practice.
See this video for a general overview on how to use positional and logical indexes to extract data from a data frame object in R
Let’s remind ourselves of the structure of the str
data frame we imported in the previous section.
<- read.table(file = 'data/nola_STR.txt', header = TRUE, sep = "\t")
nola_str ## Warning in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
## EOF within quoted string
## Warning in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
## number of items read is not a multiple of the number of columns
str(nola_str)
## 'data.frame': 23 obs. of 12 variables:
## $ Permit.Number : chr "" "" "" "" ...
## $ Address : chr "1717 Robert C Blakes, SR Dr" "1006 Race St" "2634 Louisiana Ave" "3323 Rosalie Aly" ...
## $ Permit.Type : chr "Short Term Rental Commercial Owner" "Short Term Rental Commercial Owner" "Short Term Rental Commercial Owner" "Short Term Rental Residential Owner" ...
## $ Residential.Subtype : chr "N/A" "N/A" "N/A" "Residential Partial Unit" ...
## $ Current.Status : chr "Pending" "Pending" "Pending" "Pending" ...
## $ Expiration.Date : chr "" "" "" "" ...
## $ Bedroom.Limit : int 5 5 3 1 3 1 2 2 1 2 ...
## $ Guest.Occupancy.Limit: int 10 10 6 2 6 2 4 4 2 4 ...
## $ Operator.Name : chr "Melissa Taranto" "Michael Heyne" "Michael Heyne" "Caroline Stas" ...
## $ License.Holder.Name : chr "Scott Taranto" "Boutique Hospitality" "Resonance Home LLC" "Caroline Stas" ...
## $ Application.Date : chr "8/9/22" "8/9/22" "8/9/22" "8/9/22" ...
## $ Issue_Date : chr "" "" "" "" ...
To access the data in any of the variables (columns) in our data frame we can use the $
notation. For example, to access the Bedroom.Limit
variable in our nola_str
data frame we can use nola_str$Bedroom.Limit
. This tells R that the Bedroom.Limit
variable is contained within the data frame nola_str
.
$Bedroom.Limit
nola_str## [1] 5 5 3 1 3 1 2 2 1 2 4 2 2 3 2 1 2 1 3 1 2 1 NA
This will return a vector of the Bedroom.Limit
data. If we want we can assign this vector to another object and do stuff with it, like calculate a mean or get a summary of the variable using the summary()
function.
<- nola_str$Bedroom.Limit
f_bedroom_limit mean(f_bedroom_limit)
## [1] NA
summary(f_bedroom_limit)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 1.000 1.000 2.000 2.227 3.000 5.000 1
Or if we don’t want to create an additional object we can use functions ‘on-the-fly’ to only display the value in the console.
mean(nola_str$Bedroom.Limit)
## [1] NA
summary(nola_str$Bedroom.Limit)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 1.000 1.000 2.000 2.227 3.000 5.000 1
Alternately, you can also use pipes.
$Bedroom.Limit |> mean()
nola_str## [1] NA
$Bedroom.Limit |> summary()
nola_str## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 1.000 1.000 2.000 2.227 3.000 5.000 1
Just as we did with vectors, we also can access data in data frames using the square bracket [ ]
notation. However, instead of just using a single index, we now need to use two indexes, one to specify the rows and one for the columns. To do this, we can use the notation my_data[rows, columns]
where rows
and columns
are indexes and my_data
is the name of the data frame. Again, just like with our vectors our indexes can be positional or the result of a logical test.
3.5.1 Positional indexes
To use positional indexes we simple have to write the position of the rows and columns we want to extract inside the [ ]
. For example, if for some reason we wanted to extract the first value (1st row ) of the height
variable (4th column)
1, 4]
nola_str[## [1] "N/A"
# this would give you the same
$Bedroom.Limit[1]
nola_str## [1] 5
We can also extract values from multiple rows or columns by specifying these indexes as vectors inside the [ ]
. To extract the first 10 rows and the first 4 columns we simple supply a vector containing a sequence from 1 to 10 for the rows index (1:10
) and a vector from 1 to 4 for the column index (1:4
).
1:10, 1:4]
nola_str[## Permit.Number Address
## 1 1717 Robert C Blakes, SR Dr
## 2 1006 Race St
## 3 2634 Louisiana Ave
## 4 3323 Rosalie Aly
## 5 1525 Melpomene St
## 6 22-RSTR-15568 3112 Octavia St
## 7 3149 Chartres St
## 8 1952 Treasure St
## 9 4616 S Robertson St
## 10 3043 St Philip St
## Permit.Type Residential.Subtype
## 1 Short Term Rental Commercial Owner N/A
## 2 Short Term Rental Commercial Owner N/A
## 3 Short Term Rental Commercial Owner N/A
## 4 Short Term Rental Residential Owner Residential Partial Unit
## 5 Short Term Rental Residential Owner Residential Small Unit
## 6 Short Term Rental Residential Owner Residential Partial Unit
## 7 Commercial STR N/A
## 8 Short Term Rental Residential Owner Residential Small Unit
## 9 Short Term Rental Residential Owner Residential Partial Unit
## 10 Short Term Rental Residential Owner Residential Partial Unit
Or for non sequential rows and columns then we can supply vectors of positions using the c()
function. To extract the 1st, 5th, 12th, 30th rows from the 1st, 3rd, 6th and 8th columns
c(1, 5, 12, 30), c(1, 3, 6, 8)]
nola_str[## Permit.Number Permit.Type Expiration.Date
## 1 Short Term Rental Commercial Owner
## 5 Short Term Rental Residential Owner
## 12 22-RSTR-15454 Short Term Rental Residential Owner 8/8/23
## NA <NA> <NA> <NA>
## Guest.Occupancy.Limit
## 1 10
## 5 6
## 12 4
## NA NA
All we are doing in the two examples above is creating vectors of positions for the rows and columns that we want to extract. We have done this by using the skills we developed in Chapter 2 when we generated vectors using the c()
function or using the :
notation.
But what if we want to extract either all of the rows or all of the columns? It would be extremely tedious to have to generate vectors for all rows or for all columns. Thankfully R has a shortcut. If you don’t specify either a row or column index in the [ ]
then R interprets it to mean you want all rows or all columns. For example, to extract the first 8 rows and all of the columns in the flower
data frame
1:8, ]
nola_str[## Permit.Number Address Permit.Type
## 1 1717 Robert C Blakes, SR Dr Short Term Rental Commercial Owner
## 2 1006 Race St Short Term Rental Commercial Owner
## 3 2634 Louisiana Ave Short Term Rental Commercial Owner
## 4 3323 Rosalie Aly Short Term Rental Residential Owner
## 5 1525 Melpomene St Short Term Rental Residential Owner
## 6 22-RSTR-15568 3112 Octavia St Short Term Rental Residential Owner
## 7 3149 Chartres St Commercial STR
## 8 1952 Treasure St Short Term Rental Residential Owner
## Residential.Subtype Current.Status Expiration.Date Bedroom.Limit
## 1 N/A Pending 5
## 2 N/A Pending 5
## 3 N/A Pending 3
## 4 Residential Partial Unit Pending 1
## 5 Residential Small Unit Pending 3
## 6 Residential Partial Unit Issued 8/4/23 1
## 7 N/A Pending 2
## 8 Residential Small Unit Pending 2
## Guest.Occupancy.Limit Operator.Name License.Holder.Name
## 1 10 Melissa Taranto Scott Taranto
## 2 10 Michael Heyne Boutique Hospitality
## 3 6 Michael Heyne Resonance Home LLC
## 4 2 Caroline Stas Caroline Stas
## 5 6 Craig Redgrave Craig R Redgrave
## 6 2 Philip Wheeler Philip Barrett Wheeler
## 7 4 Bruce Michael Ferweda Bruce Ferweda
## 8 4 Zedrick Price Zedrick L Price
## Application.Date Issue_Date
## 1 8/9/22
## 2 8/9/22
## 3 8/9/22
## 4 8/9/22
## 5 8/8/22
## 6 8/5/22 8/5/22
## 7 8/5/22
## 8 8/5/22
or all of the rows and the first 3 columns. If you’re reading the web version of this book scroll down in output panel to see all of the data. Note, if you’re reading the pdf version of the book some of the output has been truncated to save some space.
1:3] nola_str[,
## Permit.Number
## 1
## 2
## 3
## 4
## 5
## 6 22-RSTR-15568
## 7
## 8
## 9
## 10
## 11
## 12 22-RSTR-15454
## 13
## 14
## 15
## 16
## 17 22-CSTR-10184
## 18
## 19
## 20
## 21
## 22
## 23 22-RSTR-15236
## Address
## 1 1717 Robert C Blakes, SR Dr
## 2 1006 Race St
## 3 2634 Louisiana Ave
## 4 3323 Rosalie Aly
## 5 1525 Melpomene St
## 6 3112 Octavia St
## 7 3149 Chartres St
## 8 1952 Treasure St
## 9 4616 S Robertson St
## 10 3043 St Philip St
## 11 1727 Henriette Delille St
## 12 1731 Third St 1A
## 13 1731 Third St 1B
## 14 4859 Tchoupitoulas St
## 15 3826 Canal St
## 16 4632 Franklin Ave
## 17 3529 St Claude Ave
## 18 621 Desire St
## 19 2000 Barracks St
## 20 334 Carondelet St Unit 204
## 21 1122 St Roch Ave
## 22 334 Carondelet St Unit 404
## 23 4320 Dhemecourt St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/4/23\t3\t6\tJohnson Clawson\t"John Nathaniel Richie, Jr"\t8/1/22\t8/5/22\n\t1116 N Miro St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tMatilde Carbia\tMatilde Carbia\t8/1/22\t\n\t911 Elysian Fields Ave\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tAlan Seager\tAlan Seager\t7/31/22\t\n\t1326 Elysian Fields Ave\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t1\t2\tEddrin Williams\tEddrin Williams\t7/31/22\t\n\t11416 Prentiss Ave\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tApril Jenkins\tApril Jenkins\t7/30/22\t\n\t1029 Montegut St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t3\t6\tBettina Reutter\tBywaterbeauty guest house\t7/30/22\t\n22-RSTR-15158\t231 S White St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/8/23\t2\t4\tCarter Kronlage\tAlec Robinson\t7/29/22\t8/9/22\n\t1743 N Rocheblave St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t2\t4\tDanielle Wright\tDanielle D Wright\t7/29/22\t\n\t862 Tchoupitoulas St\tShort Term Rental Commercial Owner\tN/A\tPending\t\t2\t4\tCarter Kronlage\tTchoup N Block LLC\t7/29/22\t\n\t813 Jackson Ave\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tMichael Springer\t813 Jackson Ave\t7/29/22\t\n22-RSTR-15053\t3134 Annunciation St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/2/23\t1\t2\tPenny Nunemacher\tPenny Lynn Nunemacher\t7/28/22\t8/3/22\n\t2312 St Louis St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t3\t6\tMichael Springer\tP Rubenstein William\t7/28/22\t\n22-RSTR-15093\t1622 Gentilly Blvd\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t8/9/23\t1\t2\tCody Hammock\tCody Hammock\t7/28/22\t8/10/22\n22-CSTR-14985\t336 Camp St Apt 302\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-RSTR-15008\t1310 Touro St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t7/29/23\t2\t4\tMICHAEL CRAFT\tMichael Craft\t7/27/22\t7/30/22\n22-CSTR-14994\t336 Camp St Apt 203\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t1\t2\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14979\t336 Camp St Apt 306\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14965\t336 Camp St Apt 405\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14964\t336 Camp St Apt 406\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14980\t336 Camp St Apt 305\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14987\t336 Camp St Apt 206\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n\t734 Union St Unit 401\tShort Term Rental Commercial Owner\tN/A\tPending\t\t2\t4\tDevrim Hayes\tUnique Union Condos LLC\t7/27/22\t\n22-RSTR-14938\t1918 Laharpe St\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t7/26/23\t2\t4\tRachel Smith\tRachel Smith\t7/27/22\t7/27/22\n22-CSTR-14956\t336 Camp St Apt 404\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t1\t2\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n\t1028 Octavia St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tClaire Stockton\tClaire Stockton\t7/27/22\t\n22-CSTR-14970\t336 Camp St Apt 403\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t1\t2\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14984\t336 Camp St Apt 303\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14974\t336 Camp St Apt 401\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14998\t336 Camp St Apt 201\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14991\t336 Camp St Apt 205\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14972\t336 Camp St Apt 402\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14993\t336 Camp St Apt 204\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n\t7810 Spruce St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t2\t4\tNancy Caddigan\tNancy Caddigan\t7/27/22\t\n22-CSTR-14996\t336 Camp St Apt 202\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14981\t336 Camp St Apt 304\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n21-CSTR-13310\t888 Girod St Apt 201\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/2/23\t3\t6\tChristopher Robertson\t700 Baronne Street LLC\t7/26/22\t7/27/22\n\t7645 Forum Blvd\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t3\t6\tAUSTIN LEVY\tAustin Levy\t7/26/22\t\n22-RSTR-14862\t2204 Barracks St\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t8/8/23\t1\t2\tBrenna White\tMichael P Phillips\t7/26/22\t8/9/22\n21-CSTR-13311\t888 Girod St Apt 401\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/2/23\t3\t6\tChristopher Robertson\t700 Baronne Street LLC\t7/26/22\t7/27/22\n\t4117 St Peter St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t3\t6\tBrenna White\tBrenna White\t7/26/22\t\n21-CSTR-13312\t888 Girod St Apt 301\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/2/23\t3\t6\tChristopher Robertson\t700 Baronne Street LLC\t7/26/22\t7/27/22\n\t2532 Dante St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t1\t2\tBrenna White\tAli KP Asgar\t7/26/22\t\n\t530 Louisa St\tShort Term Rental Commercial Owner\tN/A\tPending\t\t5\t10\tCameron Tveit\tFirst Problem LLC\t7/26/22\t\n\t3918 Banks St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tMarisa Welles\tMarisa Welles\t7/26/22\t\n\t50 Fontainebleau Dr\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t3\t6\tCoulibaly Adama\tAdama Coulibaly\t7/26/22\t\n\t1011 Third St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t1\t2\tJacob Rodgers\tJacob Rodgers\t7/26/22\t\n21-RSTR-12688\t1300 Socrates St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t7/31/23\t3\t6\tBilly Lawrence\tBilly R Lawrence\t7/26/22\t8/1/22\n22-RSTR-14822\t2433 Pine St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/8/23\t1\t2\tAlleshia Fink\tMarilyn M Alexander\t7/25/22\t8/9/22\n22-RSTR-14797\t3024 Napoleon Ave Apt A\tShort Term Rental Residential Owner\tResidential Large Unit\tIssued\t8/2/23\t1\t2\tChad Jones\tChad D Jones\t7/25/22\t8/3/22\n22-RSTR-14830\t619 Franklin Ave\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t8/1/23\t1\t2\tJames Baird\tJames Baird\t7/25/22\t8/2/22\n22-RSTR-14799\t3026 Napoleon Ave\tShort Term Rental Residential Owner\tResidential Large Unit\tIssued\t8/2/23\t2\t4\tChad Jones\tChad D Jones\t7/25/22\t8/3/22\n\t1004 Verret St\tShort Term Rental Commercial Owner\tN/A\tPending\t\t2\t4\tArthur Perry\tKey Komponent Realty LLC\t7/25/22\t\n\t2721 Philip St\tShort Term Rental Residential Owner\tResidential Partial Unit\tDuplicate\t\t1\t2\tCassandra Walcott\tCassandra Walcott\t7/25/22\t\n22-RSTR-14792\t515 Short St Apt B\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t8/9/23\t1\t2\tAlexander Bollag\tAlexander Bollag\t7/25/22\t8/10/22\n\t2123 Urquhart St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t1\t2\tMarcus Smith\tKellie Byrd\t7/25/22\t\n22-RSTR-14803\t2217 Soniat St\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t7/26/23\t3\t6\tKristopher Khalil\tKristopher Khalil\t7/25/22\t7/27/22\n20-RSTR-20462\t2221 Felicity St\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t6/29/23\t2\t4\tShuneki Williams\tShuneki Williams\t7/25/22\t7/30/22\n\t8939 Nelson St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t2\t4\tRaymond Stevenson\tStevenson Raymond\t7/25/22\t\n\t4302 Banks St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t2\t4\tWilson Blum\tWilson Blum\t7/24/22\t\n\t2363 N Villere St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t1\t2\tLexie Cole\tLexie Cole\t7/24/22\t\n\t1648 N Tonti St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tKaitlyn Gaddis Thompson\tKaitlyn Gaddis\t7/24/22\t\n22-RSTR-14732\t113 S Salcedo St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/2/23\t1\t2\tMichael Springer\tStarr Nia\t7/24/22\t8/3/22\n22-RSTR-14693\t1212 Mazant St A\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t7/31/23\t3\t6\tJoseph Kennedy\tJoseph Kennedy\t7/23/22\t8/1/22\n\t2421 Chartres St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t2\t4\tMarlene Sheely\tMarlene Sheely\t7/23/22\t\n22-RSTR-14696\t1214 Mazant St A\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t7/31/23\t3\t6\tJoseph Kennedy\tJoseph Kennedy\t7/23/22\t8/1/22\n\t814 Baronne St\tShort Term Rental Commercial Owner\tN/A\tPending\t\t3\t6\tDanae Columbus\tDanae J Columbus\t7/22/22\t\n22-RSTR-14667\t823 Mandeville St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/1/23\t2\t4\tRachel Anthony\tRachel Anthony\t7/22/22\t8/2/22
## Permit.Type
## 1 Short Term Rental Commercial Owner
## 2 Short Term Rental Commercial Owner
## 3 Short Term Rental Commercial Owner
## 4 Short Term Rental Residential Owner
## 5 Short Term Rental Residential Owner
## 6 Short Term Rental Residential Owner
## 7 Commercial STR
## 8 Short Term Rental Residential Owner
## 9 Short Term Rental Residential Owner
## 10 Short Term Rental Residential Owner
## 11 Short Term Rental Commercial Owner
## 12 Short Term Rental Residential Owner
## 13 Short Term Rental Residential Owner
## 14 Short Term Rental Commercial Owner
## 15 Short Term Rental Commercial Owner
## 16 Short Term Rental Residential Owner
## 17 Short Term Rental Commercial Owner
## 18 Short Term Rental Residential Owner
## 19 Short Term Rental Residential Owner
## 20 Short Term Rental Commercial Owner
## 21 Short Term Rental Commercial Owner
## 22 Short Term Rental Commercial Owner
## 23
We can even use negative positional indexes to exclude certain rows and columns. As an example, lets extract all of the rows except the first 85 rows and all columns except the 4th, 7th and 8th columns. Notice we need to use -()
when we generate our row positional vectors. If we had just used -1:85
this would actually generate a regular sequence from -1 to 85 which is not what we want (we can of course use -1:-85
).
-(1:85), -c(4, 7, 8)]
nola_str[## [1] Permit.Number Address Permit.Type
## [4] Current.Status Expiration.Date Operator.Name
## [7] License.Holder.Name Application.Date Issue_Date
## <0 rows> (or 0-length row.names)
In addition to using a positional index for extracting particular columns (variables) we can also name the variables directly when using the square bracket [ ]
notation. For example, let’s extract the first 5 rows and the variables treat
, nitrogen
and leafarea
. Instead of using str[1:5, c(1, 2, 6)]
we can instead use
1:5, c("Operator.Name", "License.Holder.Name", "Application.Date")]
nola_str[## Operator.Name License.Holder.Name Application.Date
## 1 Melissa Taranto Scott Taranto 8/9/22
## 2 Michael Heyne Boutique Hospitality 8/9/22
## 3 Michael Heyne Resonance Home LLC 8/9/22
## 4 Caroline Stas Caroline Stas 8/9/22
## 5 Craig Redgrave Craig R Redgrave 8/8/22
We often use this method in preference to the positional index for selecting columns as it will still give us what we want even if we’ve changed the order of the columns in our data frame for some reason.
3.5.2 Logical indexes
Just as we did with vectors, we can also extract data from our data frame based on a logical test. We can use all of the logical operators that we used for our vector examples so if these have slipped your mind maybe pop back and refresh your memory. Let’s extract all rows where Bedroom.Limit
is greater than 3 and extract all columns by default (remember, if you don’t include a column index after the comma it means all columns).
<- nola_str[nola_str$Bedroom.Limit > 3, ]
big_str
big_str## Permit.Number Address Permit.Type
## 1 1717 Robert C Blakes, SR Dr Short Term Rental Commercial Owner
## 2 1006 Race St Short Term Rental Commercial Owner
## 11 1727 Henriette Delille St Short Term Rental Commercial Owner
## NA <NA> <NA> <NA>
## Residential.Subtype Current.Status Expiration.Date Bedroom.Limit
## 1 N/A Pending 5
## 2 N/A Pending 5
## 11 N/A Pending 4
## NA <NA> <NA> <NA> NA
## Guest.Occupancy.Limit Operator.Name License.Holder.Name Application.Date
## 1 10 Melissa Taranto Scott Taranto 8/9/22
## 2 10 Michael Heyne Boutique Hospitality 8/9/22
## 11 8 Jane Chaisson David Trocquet Jr. 8/4/22
## NA NA <NA> <NA> <NA>
## Issue_Date
## 1
## 2
## 11
## NA <NA>
Notice in the code above that we need to use the nola_str$Bedroom.Limit
notation for the logical test. If we just named the Bedroom.Limit
variable without the name of the data frame we would receive an error telling us R couldn’t find the variable nola_str$Bedroom.Limit
. The reason for this is that the nola_str$Bedroom.Limit
variable only exists inside the nola_str
data frame so you need to tell R exactly where it is.
<- nola_str[Bedroom.Limit > 3, ]
big_str in `[.data.frame`(nola_str, Bedroom.Limit > 3, ) :
Error 'Bedroom.Limit' not found object
So how does this work? The logical test is nola_str$Bedroom.Limit > 3
and R will only extract those rows that satisfy this logical condition. If we look at the output of just the logical condition you can see this returns a vector containing TRUE
if Bedroom.Limit
is greater than 3 and FALSE
if Bedroom.Limit
is not greater than 3.
$Bedroom.Limit > 3
nola_str## [1] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE NA
So our row index is a vector containing either TRUE
or FALSE
values and only those rows that are TRUE
are selected.
Other commonly used operators are shown below
$Bedroom.Limit >= 3, ] # values greater or equal to 3
nola_str[nola_str
$Bedroom.Limit <= 3, ] # values less than or equal to 3
nola_str[nola_str
$Bedroom.Limit <= 4, ] # values equal to 4
nola_str[nola_str
$Bedroom.Limit != 4, ] # values not equal to 4 nola_str[nola_str
We can also extract rows based on the value of a character string or factor level. Let’s extract all rows where the Operator.Name
is equal to Michael Heyne
(again we will output all columns). Notice that the double equals ==
sign must be used for a logical test and that the character string must be enclosed in either single or double quotes (i.e. "Michael Heyne"
).
<- nola_str[nola_str$Operator.Name == "Michael Heyne", ]
k k
## Permit.Number Address Permit.Type
## 2 1006 Race St Short Term Rental Commercial Owner
## 3 2634 Louisiana Ave Short Term Rental Commercial Owner
## Residential.Subtype Current.Status Expiration.Date Bedroom.Limit
## 2 N/A Pending 5
## 3 N/A Pending 3
## Guest.Occupancy.Limit Operator.Name License.Holder.Name Application.Date
## 2 10 Michael Heyne Boutique Hospitality 8/9/22
## 3 6 Michael Heyne Resonance Home LLC 8/9/22
## Issue_Date
## 2
## 3
Or we can extract all rows where Current.Status
is not equal to Pending
(using !=
) and only return columns 1 to 4.
<- nola_str[nola_str$Current.Status != "Pending", 1:4]
nola_str_notPending nola_str_notPending
## Permit.Number
## 6 22-RSTR-15568
## 12 22-RSTR-15454
## 17 22-CSTR-10184
## 23 22-RSTR-15236
## Address
## 6 3112 Octavia St
## 12 1731 Third St 1A
## 17 3529 St Claude Ave
## 23 4320 Dhemecourt St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/4/23\t3\t6\tJohnson Clawson\t"John Nathaniel Richie, Jr"\t8/1/22\t8/5/22\n\t1116 N Miro St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tMatilde Carbia\tMatilde Carbia\t8/1/22\t\n\t911 Elysian Fields Ave\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tAlan Seager\tAlan Seager\t7/31/22\t\n\t1326 Elysian Fields Ave\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t1\t2\tEddrin Williams\tEddrin Williams\t7/31/22\t\n\t11416 Prentiss Ave\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tApril Jenkins\tApril Jenkins\t7/30/22\t\n\t1029 Montegut St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t3\t6\tBettina Reutter\tBywaterbeauty guest house\t7/30/22\t\n22-RSTR-15158\t231 S White St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/8/23\t2\t4\tCarter Kronlage\tAlec Robinson\t7/29/22\t8/9/22\n\t1743 N Rocheblave St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t2\t4\tDanielle Wright\tDanielle D Wright\t7/29/22\t\n\t862 Tchoupitoulas St\tShort Term Rental Commercial Owner\tN/A\tPending\t\t2\t4\tCarter Kronlage\tTchoup N Block LLC\t7/29/22\t\n\t813 Jackson Ave\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tMichael Springer\t813 Jackson Ave\t7/29/22\t\n22-RSTR-15053\t3134 Annunciation St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/2/23\t1\t2\tPenny Nunemacher\tPenny Lynn Nunemacher\t7/28/22\t8/3/22\n\t2312 St Louis St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t3\t6\tMichael Springer\tP Rubenstein William\t7/28/22\t\n22-RSTR-15093\t1622 Gentilly Blvd\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t8/9/23\t1\t2\tCody Hammock\tCody Hammock\t7/28/22\t8/10/22\n22-CSTR-14985\t336 Camp St Apt 302\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-RSTR-15008\t1310 Touro St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t7/29/23\t2\t4\tMICHAEL CRAFT\tMichael Craft\t7/27/22\t7/30/22\n22-CSTR-14994\t336 Camp St Apt 203\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t1\t2\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14979\t336 Camp St Apt 306\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14965\t336 Camp St Apt 405\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14964\t336 Camp St Apt 406\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14980\t336 Camp St Apt 305\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14987\t336 Camp St Apt 206\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n\t734 Union St Unit 401\tShort Term Rental Commercial Owner\tN/A\tPending\t\t2\t4\tDevrim Hayes\tUnique Union Condos LLC\t7/27/22\t\n22-RSTR-14938\t1918 Laharpe St\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t7/26/23\t2\t4\tRachel Smith\tRachel Smith\t7/27/22\t7/27/22\n22-CSTR-14956\t336 Camp St Apt 404\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t1\t2\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n\t1028 Octavia St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tClaire Stockton\tClaire Stockton\t7/27/22\t\n22-CSTR-14970\t336 Camp St Apt 403\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t1\t2\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14984\t336 Camp St Apt 303\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14974\t336 Camp St Apt 401\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14998\t336 Camp St Apt 201\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14991\t336 Camp St Apt 205\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14972\t336 Camp St Apt 402\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14993\t336 Camp St Apt 204\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n\t7810 Spruce St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t2\t4\tNancy Caddigan\tNancy Caddigan\t7/27/22\t\n22-CSTR-14996\t336 Camp St Apt 202\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14981\t336 Camp St Apt 304\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n21-CSTR-13310\t888 Girod St Apt 201\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/2/23\t3\t6\tChristopher Robertson\t700 Baronne Street LLC\t7/26/22\t7/27/22\n\t7645 Forum Blvd\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t3\t6\tAUSTIN LEVY\tAustin Levy\t7/26/22\t\n22-RSTR-14862\t2204 Barracks St\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t8/8/23\t1\t2\tBrenna White\tMichael P Phillips\t7/26/22\t8/9/22\n21-CSTR-13311\t888 Girod St Apt 401\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/2/23\t3\t6\tChristopher Robertson\t700 Baronne Street LLC\t7/26/22\t7/27/22\n\t4117 St Peter St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t3\t6\tBrenna White\tBrenna White\t7/26/22\t\n21-CSTR-13312\t888 Girod St Apt 301\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/2/23\t3\t6\tChristopher Robertson\t700 Baronne Street LLC\t7/26/22\t7/27/22\n\t2532 Dante St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t1\t2\tBrenna White\tAli KP Asgar\t7/26/22\t\n\t530 Louisa St\tShort Term Rental Commercial Owner\tN/A\tPending\t\t5\t10\tCameron Tveit\tFirst Problem LLC\t7/26/22\t\n\t3918 Banks St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tMarisa Welles\tMarisa Welles\t7/26/22\t\n\t50 Fontainebleau Dr\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t3\t6\tCoulibaly Adama\tAdama Coulibaly\t7/26/22\t\n\t1011 Third St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t1\t2\tJacob Rodgers\tJacob Rodgers\t7/26/22\t\n21-RSTR-12688\t1300 Socrates St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t7/31/23\t3\t6\tBilly Lawrence\tBilly R Lawrence\t7/26/22\t8/1/22\n22-RSTR-14822\t2433 Pine St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/8/23\t1\t2\tAlleshia Fink\tMarilyn M Alexander\t7/25/22\t8/9/22\n22-RSTR-14797\t3024 Napoleon Ave Apt A\tShort Term Rental Residential Owner\tResidential Large Unit\tIssued\t8/2/23\t1\t2\tChad Jones\tChad D Jones\t7/25/22\t8/3/22\n22-RSTR-14830\t619 Franklin Ave\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t8/1/23\t1\t2\tJames Baird\tJames Baird\t7/25/22\t8/2/22\n22-RSTR-14799\t3026 Napoleon Ave\tShort Term Rental Residential Owner\tResidential Large Unit\tIssued\t8/2/23\t2\t4\tChad Jones\tChad D Jones\t7/25/22\t8/3/22\n\t1004 Verret St\tShort Term Rental Commercial Owner\tN/A\tPending\t\t2\t4\tArthur Perry\tKey Komponent Realty LLC\t7/25/22\t\n\t2721 Philip St\tShort Term Rental Residential Owner\tResidential Partial Unit\tDuplicate\t\t1\t2\tCassandra Walcott\tCassandra Walcott\t7/25/22\t\n22-RSTR-14792\t515 Short St Apt B\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t8/9/23\t1\t2\tAlexander Bollag\tAlexander Bollag\t7/25/22\t8/10/22\n\t2123 Urquhart St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t1\t2\tMarcus Smith\tKellie Byrd\t7/25/22\t\n22-RSTR-14803\t2217 Soniat St\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t7/26/23\t3\t6\tKristopher Khalil\tKristopher Khalil\t7/25/22\t7/27/22\n20-RSTR-20462\t2221 Felicity St\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t6/29/23\t2\t4\tShuneki Williams\tShuneki Williams\t7/25/22\t7/30/22\n\t8939 Nelson St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t2\t4\tRaymond Stevenson\tStevenson Raymond\t7/25/22\t\n\t4302 Banks St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t2\t4\tWilson Blum\tWilson Blum\t7/24/22\t\n\t2363 N Villere St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t1\t2\tLexie Cole\tLexie Cole\t7/24/22\t\n\t1648 N Tonti St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tKaitlyn Gaddis Thompson\tKaitlyn Gaddis\t7/24/22\t\n22-RSTR-14732\t113 S Salcedo St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/2/23\t1\t2\tMichael Springer\tStarr Nia\t7/24/22\t8/3/22\n22-RSTR-14693\t1212 Mazant St A\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t7/31/23\t3\t6\tJoseph Kennedy\tJoseph Kennedy\t7/23/22\t8/1/22\n\t2421 Chartres St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t2\t4\tMarlene Sheely\tMarlene Sheely\t7/23/22\t\n22-RSTR-14696\t1214 Mazant St A\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t7/31/23\t3\t6\tJoseph Kennedy\tJoseph Kennedy\t7/23/22\t8/1/22\n\t814 Baronne St\tShort Term Rental Commercial Owner\tN/A\tPending\t\t3\t6\tDanae Columbus\tDanae J Columbus\t7/22/22\t\n22-RSTR-14667\t823 Mandeville St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/1/23\t2\t4\tRachel Anthony\tRachel Anthony\t7/22/22\t8/2/22
## Permit.Type Residential.Subtype
## 6 Short Term Rental Residential Owner Residential Partial Unit
## 12 Short Term Rental Residential Owner Residential Large Unit
## 17 Short Term Rental Commercial Owner N/A
## 23
We can increase the complexity of our logical tests by combining them with Boolean expressions just as we did for vector objects. For example, to extract all rows where Bedroom.Limit
is greater or equal to 3
AND Current.Status
is equal to Pending
AND Permit.Type
is equal to Short Term Rental Residential Owner"
we combine a series of logical expressions with the &
symbol.
<- nola_str[nola_str$Bedroom.Limit >= 3 &
k2 $Current.Status == "Pending" &
nola_str$Permit.Type == "Short Term Rental Residential Owner",
nola_str
] 1:5]
k2[,## Permit.Number Address Permit.Type
## 5 1525 Melpomene St Short Term Rental Residential Owner
## 19 2000 Barracks St Short Term Rental Residential Owner
## Residential.Subtype Current.Status
## 5 Residential Small Unit Pending
## 19 Residential Partial Unit Pending
To extract rows based on an ‘OR’ Boolean expression we can use the |
symbol. Don’t forget the ,
to specify and extract the column indices.
<- nola_str[nola_str$Bedroom.Limit >= 3 |
k3 $Current.Status != "Pending",
nola_str1:3]
k3## Permit.Number
## 1
## 2
## 3
## 5
## 6 22-RSTR-15568
## 11
## 12 22-RSTR-15454
## 14
## 17 22-CSTR-10184
## 19
## 23 22-RSTR-15236
## Address
## 1 1717 Robert C Blakes, SR Dr
## 2 1006 Race St
## 3 2634 Louisiana Ave
## 5 1525 Melpomene St
## 6 3112 Octavia St
## 11 1727 Henriette Delille St
## 12 1731 Third St 1A
## 14 4859 Tchoupitoulas St
## 17 3529 St Claude Ave
## 19 2000 Barracks St
## 23 4320 Dhemecourt St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/4/23\t3\t6\tJohnson Clawson\t"John Nathaniel Richie, Jr"\t8/1/22\t8/5/22\n\t1116 N Miro St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tMatilde Carbia\tMatilde Carbia\t8/1/22\t\n\t911 Elysian Fields Ave\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tAlan Seager\tAlan Seager\t7/31/22\t\n\t1326 Elysian Fields Ave\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t1\t2\tEddrin Williams\tEddrin Williams\t7/31/22\t\n\t11416 Prentiss Ave\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tApril Jenkins\tApril Jenkins\t7/30/22\t\n\t1029 Montegut St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t3\t6\tBettina Reutter\tBywaterbeauty guest house\t7/30/22\t\n22-RSTR-15158\t231 S White St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/8/23\t2\t4\tCarter Kronlage\tAlec Robinson\t7/29/22\t8/9/22\n\t1743 N Rocheblave St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t2\t4\tDanielle Wright\tDanielle D Wright\t7/29/22\t\n\t862 Tchoupitoulas St\tShort Term Rental Commercial Owner\tN/A\tPending\t\t2\t4\tCarter Kronlage\tTchoup N Block LLC\t7/29/22\t\n\t813 Jackson Ave\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tMichael Springer\t813 Jackson Ave\t7/29/22\t\n22-RSTR-15053\t3134 Annunciation St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/2/23\t1\t2\tPenny Nunemacher\tPenny Lynn Nunemacher\t7/28/22\t8/3/22\n\t2312 St Louis St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t3\t6\tMichael Springer\tP Rubenstein William\t7/28/22\t\n22-RSTR-15093\t1622 Gentilly Blvd\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t8/9/23\t1\t2\tCody Hammock\tCody Hammock\t7/28/22\t8/10/22\n22-CSTR-14985\t336 Camp St Apt 302\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-RSTR-15008\t1310 Touro St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t7/29/23\t2\t4\tMICHAEL CRAFT\tMichael Craft\t7/27/22\t7/30/22\n22-CSTR-14994\t336 Camp St Apt 203\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t1\t2\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14979\t336 Camp St Apt 306\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14965\t336 Camp St Apt 405\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14964\t336 Camp St Apt 406\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14980\t336 Camp St Apt 305\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14987\t336 Camp St Apt 206\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n\t734 Union St Unit 401\tShort Term Rental Commercial Owner\tN/A\tPending\t\t2\t4\tDevrim Hayes\tUnique Union Condos LLC\t7/27/22\t\n22-RSTR-14938\t1918 Laharpe St\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t7/26/23\t2\t4\tRachel Smith\tRachel Smith\t7/27/22\t7/27/22\n22-CSTR-14956\t336 Camp St Apt 404\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t1\t2\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n\t1028 Octavia St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tClaire Stockton\tClaire Stockton\t7/27/22\t\n22-CSTR-14970\t336 Camp St Apt 403\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t1\t2\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14984\t336 Camp St Apt 303\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14974\t336 Camp St Apt 401\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14998\t336 Camp St Apt 201\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14991\t336 Camp St Apt 205\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14972\t336 Camp St Apt 402\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14993\t336 Camp St Apt 204\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n\t7810 Spruce St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t2\t4\tNancy Caddigan\tNancy Caddigan\t7/27/22\t\n22-CSTR-14996\t336 Camp St Apt 202\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n22-CSTR-14981\t336 Camp St Apt 304\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/8/23\t2\t4\tCesar Burgos\t336 Camp LLC\t7/27/22\t8/9/22\n21-CSTR-13310\t888 Girod St Apt 201\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/2/23\t3\t6\tChristopher Robertson\t700 Baronne Street LLC\t7/26/22\t7/27/22\n\t7645 Forum Blvd\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t3\t6\tAUSTIN LEVY\tAustin Levy\t7/26/22\t\n22-RSTR-14862\t2204 Barracks St\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t8/8/23\t1\t2\tBrenna White\tMichael P Phillips\t7/26/22\t8/9/22\n21-CSTR-13311\t888 Girod St Apt 401\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/2/23\t3\t6\tChristopher Robertson\t700 Baronne Street LLC\t7/26/22\t7/27/22\n\t4117 St Peter St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t3\t6\tBrenna White\tBrenna White\t7/26/22\t\n21-CSTR-13312\t888 Girod St Apt 301\tShort Term Rental Commercial Owner\tN/A\tIssued\t8/2/23\t3\t6\tChristopher Robertson\t700 Baronne Street LLC\t7/26/22\t7/27/22\n\t2532 Dante St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t1\t2\tBrenna White\tAli KP Asgar\t7/26/22\t\n\t530 Louisa St\tShort Term Rental Commercial Owner\tN/A\tPending\t\t5\t10\tCameron Tveit\tFirst Problem LLC\t7/26/22\t\n\t3918 Banks St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tMarisa Welles\tMarisa Welles\t7/26/22\t\n\t50 Fontainebleau Dr\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t3\t6\tCoulibaly Adama\tAdama Coulibaly\t7/26/22\t\n\t1011 Third St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t1\t2\tJacob Rodgers\tJacob Rodgers\t7/26/22\t\n21-RSTR-12688\t1300 Socrates St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t7/31/23\t3\t6\tBilly Lawrence\tBilly R Lawrence\t7/26/22\t8/1/22\n22-RSTR-14822\t2433 Pine St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/8/23\t1\t2\tAlleshia Fink\tMarilyn M Alexander\t7/25/22\t8/9/22\n22-RSTR-14797\t3024 Napoleon Ave Apt A\tShort Term Rental Residential Owner\tResidential Large Unit\tIssued\t8/2/23\t1\t2\tChad Jones\tChad D Jones\t7/25/22\t8/3/22\n22-RSTR-14830\t619 Franklin Ave\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t8/1/23\t1\t2\tJames Baird\tJames Baird\t7/25/22\t8/2/22\n22-RSTR-14799\t3026 Napoleon Ave\tShort Term Rental Residential Owner\tResidential Large Unit\tIssued\t8/2/23\t2\t4\tChad Jones\tChad D Jones\t7/25/22\t8/3/22\n\t1004 Verret St\tShort Term Rental Commercial Owner\tN/A\tPending\t\t2\t4\tArthur Perry\tKey Komponent Realty LLC\t7/25/22\t\n\t2721 Philip St\tShort Term Rental Residential Owner\tResidential Partial Unit\tDuplicate\t\t1\t2\tCassandra Walcott\tCassandra Walcott\t7/25/22\t\n22-RSTR-14792\t515 Short St Apt B\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t8/9/23\t1\t2\tAlexander Bollag\tAlexander Bollag\t7/25/22\t8/10/22\n\t2123 Urquhart St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t1\t2\tMarcus Smith\tKellie Byrd\t7/25/22\t\n22-RSTR-14803\t2217 Soniat St\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t7/26/23\t3\t6\tKristopher Khalil\tKristopher Khalil\t7/25/22\t7/27/22\n20-RSTR-20462\t2221 Felicity St\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t6/29/23\t2\t4\tShuneki Williams\tShuneki Williams\t7/25/22\t7/30/22\n\t8939 Nelson St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t2\t4\tRaymond Stevenson\tStevenson Raymond\t7/25/22\t\n\t4302 Banks St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t2\t4\tWilson Blum\tWilson Blum\t7/24/22\t\n\t2363 N Villere St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t1\t2\tLexie Cole\tLexie Cole\t7/24/22\t\n\t1648 N Tonti St\tShort Term Rental Residential Owner\tResidential Partial Unit\tPending\t\t2\t4\tKaitlyn Gaddis Thompson\tKaitlyn Gaddis\t7/24/22\t\n22-RSTR-14732\t113 S Salcedo St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/2/23\t1\t2\tMichael Springer\tStarr Nia\t7/24/22\t8/3/22\n22-RSTR-14693\t1212 Mazant St A\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t7/31/23\t3\t6\tJoseph Kennedy\tJoseph Kennedy\t7/23/22\t8/1/22\n\t2421 Chartres St\tShort Term Rental Residential Owner\tResidential Small Unit\tPending\t\t2\t4\tMarlene Sheely\tMarlene Sheely\t7/23/22\t\n22-RSTR-14696\t1214 Mazant St A\tShort Term Rental Residential Owner\tResidential Small Unit\tIssued\t7/31/23\t3\t6\tJoseph Kennedy\tJoseph Kennedy\t7/23/22\t8/1/22\n\t814 Baronne St\tShort Term Rental Commercial Owner\tN/A\tPending\t\t3\t6\tDanae Columbus\tDanae J Columbus\t7/22/22\t\n22-RSTR-14667\t823 Mandeville St\tShort Term Rental Residential Owner\tResidential Partial Unit\tIssued\t8/1/23\t2\t4\tRachel Anthony\tRachel Anthony\t7/22/22\t8/2/22
## Permit.Type
## 1 Short Term Rental Commercial Owner
## 2 Short Term Rental Commercial Owner
## 3 Short Term Rental Commercial Owner
## 5 Short Term Rental Residential Owner
## 6 Short Term Rental Residential Owner
## 11 Short Term Rental Commercial Owner
## 12 Short Term Rental Residential Owner
## 14 Short Term Rental Commercial Owner
## 17 Short Term Rental Commercial Owner
## 19 Short Term Rental Residential Owner
## 23
3.5.3 Adding columns and rows
Sometimes it’s useful to be able to add extra rows and columns of data to our data frames. There are multiple ways to achieve this (as there always is in R!) depending on your circumstances. To simply append additional rows to an existing data frame we can use the rbind()
function and to append columns the cbind()
function. Let’s create a couple of test data frames to see this in action using our old friend the data.frame()
function.
# rbind for rows
<- data.frame(id = 1:4, height = c(120, 150, 132, 122),
df1 weight = c(44, 56, 49, 45))
df1## id height weight
## 1 1 120 44
## 2 2 150 56
## 3 3 132 49
## 4 4 122 45
<- data.frame(id = 5:6, height = c(119, 110),
df2 weight = c(39, 35))
df2## id height weight
## 1 5 119 39
## 2 6 110 35
<- data.frame(id = 1:4, height = c(120, 150, 132, 122),
df3 weight = c(44, 56, 49, 45))
df3## id height weight
## 1 1 120 44
## 2 2 150 56
## 3 3 132 49
## 4 4 122 45
<- data.frame(location = c("UK", "CZ", "CZ", "UK"))
df4
df4## location
## 1 UK
## 2 CZ
## 3 CZ
## 4 UK
We can use the rbind()
function to append the rows of data in df2
to the rows in df1
and assign the new data frame to df_rcomb
.
<- rbind(df1, df2)
df_rcomb
df_rcomb## id height weight
## 1 1 120 44
## 2 2 150 56
## 3 3 132 49
## 4 4 122 45
## 5 5 119 39
## 6 6 110 35
And cbind
to append the column in df4
to the df3
data frame and assign to df_ccomb`.
<- cbind(df3, df4)
df_ccomb
df_ccomb## id height weight location
## 1 1 120 44 UK
## 2 2 150 56 CZ
## 3 3 132 49 CZ
## 4 4 122 45 UK
Another situation when adding a new column to a data frame is useful is when you want to perform some kind of transformation on an existing variable. For example, say we wanted to apply a log10 transformation on the height variable in the df_rcomb
data frame we created above. We could just create a separate variable to contains these values but it’s good practice to create this variable as a new column inside our existing data frame so we keep all of our data together. Let’s call this new variable height_log10
.
# log10 transformation
$height_log10 <- log10(df_rcomb$height)
df_rcomb
df_rcomb## id height weight height_log10
## 1 1 120 44 2.079181
## 2 2 150 56 2.176091
## 3 3 132 49 2.120574
## 4 4 122 45 2.086360
## 5 5 119 39 2.075547
## 6 6 110 35 2.041393
This situation also crops up when we want to convert an existing variable in a data frame from one data class to another data class. For example, the id
variable in the df_rcomb
data frame is numeric type data (use the str()
or class()
functions to check for yourself). If we wanted to convert the id
variable to a factor to use later in our analysis we can create a new variable called Fid
in our data frame and use the factor()
function to convert the id
variable.
# convert to a factor
$Fid <- factor(df_rcomb$id)
df_rcomb
df_rcomb## id height weight height_log10 Fid
## 1 1 120 44 2.079181 1
## 2 2 150 56 2.176091 2
## 3 3 132 49 2.120574 3
## 4 4 122 45 2.086360 4
## 5 5 119 39 2.075547 5
## 6 6 110 35 2.041393 6
str(df_rcomb)
## 'data.frame': 6 obs. of 5 variables:
## $ id : int 1 2 3 4 5 6
## $ height : num 120 150 132 122 119 110
## $ weight : num 44 56 49 45 39 35
## $ height_log10: num 2.08 2.18 2.12 2.09 2.08 ...
## $ Fid : Factor w/ 6 levels "1","2","3","4",..: 1 2 3 4 5 6