Watts-College / cpp-529-spr-2022

https://watts-college.github.io/cpp-529-spr-2022/
0 stars 2 forks source link

Lab 02 - Transforming the Data #3

Open Rami-Assaad opened 2 years ago

Rami-Assaad commented 2 years ago

Hello , I'm stuck in step 02, when I'm running the following code :

CenDF<-CenDF %>% mutate(variable=case_when( variable=="B19013_001" ~ "MedIncome", variable=="B25077_001" ~ "MedValue")) %>% select(-moe) %>% spread(variable, estimate) %>% #Spread moves rows into columns mutate(HHInc_HousePrice_Ratio=round(MedValue/MedIncome,2)

The following error appears: Error: Each row of output must be identified by a unique combination of keys. Keys are shared for 6440 rows: * 1, 2 * 3, 4 * 5, 6 * 7, 8 * 9, 10 * 11, 12 * 13, 14 * 15, 16 * 17, 18 * 19, 20 * 21, 22 * 23, 24 * 25, 26 * 27, 28 * 29, 30 * 31, 32 * 33, 34 * 35, 36 * 37, 38 * 39, 40 * 41, 42 * 43, 44 * 45, 46 * 47, 48 * 49, 50 * 51, 52 * 53, 54 * 55, 56 * 57, 58 * 59, 60 * 61, 62 * 63, 64 * 65, 66 * 67, 68 * 69, 70 * 71, 72 * 73, 74 * 75, 76 * 77, 78 * 79, 80 * 81, 82 * 83, 84 * 85, 86 * 87, 88 * 89, 90 * 91, 92 * 93, 94 * 95, 96 * 97, 98 * 99, 100 * 101, 102 * 103, 104 * 105, 106 * 107, 108 * 109, 110 * 111, 112 * 113, 114 * 115, 116 * 117, 118 * 119, 120 * 121, 122 * 123, 124 * 125, 126 * 127, 128 * 129, 130 * 131, 132 * 133, 134 * 135, 136 * 137, 138 * 139, 140 * 141, 142 * 143, 144 * 145, 146 * 147, 148 * 149, 150 * 151, 152 * 153, 154 * 155, 156 * 157, 158 * 159, 160 * 161, 162 * 163, 164 * 165, 166 * 167, 168 * 169, 170 * 171, 172 * 173, 174 * 175, 176 * 177, 178 * 179, 180 * 181, 18

And then can’t proceed with the next step since HHInc_HousePrice_Ratio is not defined: Error in order(CenDF$HHInc_HousePrice_Ratio) : argument 1 is not a vector

What might be the issue ?

JasonSills commented 2 years ago

Hi @Rami-Assaad,

Let's try this. First, go back to step 1 and make sure we've created the variables. Let's create a code chunk where we create variable and the CenDF. I've changed variable names to "YOUR VARIABLE NAME HERE" so you can name these. Let me know if this works.

YOUR VARIABLE NAME HERE <- c(YOUR VARIABLE NAME HERE= "B25077_001", 
              YOUR VARIABLE NAME HERE = "B19013_001")
CenDF <- get_acs(geography="county", 
                year=2017, 
                survey="acs5", 
                variables= YOUR VARIABLE NAME HERE,
                geometry=T)
head(CenDF)

Then move to step 2, transforming the data:

library(tidyr)
CenDF <- CenDF %>% 
  mutate(variable=case_when( 
    variable=="YOUR VARIABLE NAME HERE" ~ "HouseValue",
    variable=="YOUR VARIABLE NAME HERE" ~ "HHIncome")) %>%
  select(-moe) %>%  
  spread(variable, estimate) %>%  #Spread moves rows into columns
  mutate(HHInc_HousePrice_Ratio=round(HouseValue/HHIncome,2)) 
droach7 commented 2 years ago

@JasonSills

I tried adapting your code but kept having an issue where it said that the variable for median house value didn't exist.

Error in stopifnot(!inherits(x, "sf"), !missing(sf_column_name), !missing(agr)) : Caused by error: ! object 'home.value' not found

Here is the code I am trying to run that produces that error:

home.info <- c(home.value = "B25077_001", home.income = "B19013_001")

CenDF <- get_acs(geography="county", 
                year=2017, 
                survey="acs5", 
                variables= home.info,
                geometry=T)
head(CenDF)

# Step 2: Transforming the data
CenDF <- CenDF %>% 
  mutate(variable=case_when( 
    variable=="home.value" ~ "HouseValue",
    variable=="home.income" ~ "HHIncome")) %>%
  select(-moe) %>%  
  spread(variable, estimate) %>%  #Spread moves rows into columns
  mutate(HHInc_HousePrice_Ratio=round(home.value/home.income, 2)) 

Could you please help?

JasonSills commented 2 years ago

@droach7 Try renaming your values in the ratio to the names assigned in the two variable lines: mutate(HHInc_HousePrice_Ratio=round(HouseValue/HHIncome, 2))