Open hyunkwanjungkr opened 9 months ago
@JasonSills ^
What code chunk would pull up the answer directly instead of a table that needed to be searched through?
@hyunkwanjungkr I believe with issues we were instructed to tag Professor Sills so he gets an email notification to check.
@hyunkwanjungkr For this assignment you only need to create a table with all names and identify the one with highest number. Report the name of the neighborhood. You will have to read the table to report the neighborhood with the highest count of vacant land.
@swest235 is requesting code that would answer more directly. Here are a couple of ways you could find the neighborhood. Both methods are beyond the current lab, so it is only to demonstrate what we can do in R. You do not need to replicate these in the homework.
Let's start with creating a table and then sorting it. Create the table we are interested in with neighborhood and land use, just as you would do to print out the table for neighborhood and land_use. Now let's create an object with this table. I'm naming the object "tab" and using the same function we used for creating the basic table (table(dat$neighborhood, dat$land_use)). Then I need to create a new object I'm calling "sortedtab" in the code below. The code orders the data by "Vacant Land" and I'm choosing to order it in decreasing order. Then I'm simply typing "sortedtab" to print. This will sort by Vacant Land and you can see the neighborhood and count at the top of the list.
tab <- table(dat$neighborhood, dat$land_use) # first step
sortedtab <- tab[order(tab[,"Vacant Land"], decreasing = TRUE),] # second step
sortedtab # print second step
Alternatively, you could use the which.max function and filter the land_use vector for "Vacant Land":
which.max(table(dat$neighborhood,
dat$land_use == "Vacant Land")[ , 2])
This will return the neighborhood with the maximum land use for only "Vacant Land". Note, however, that it only returns the name of the neighborhood, not the count of "Vacant Land".
You will see more options in this week's solutions.
Just showing a table is enough? or do I have to answer the name of the neighborhood only? It is hard to find the function for selecting only the name from the table() function's result with two variables.