brad-cannell / r4epi

Repository for the R for Epidemiology book
http://www.r4epi.com/
Other
18 stars 50 forks source link

Discuss the error from performing mathematical operations on character vectors #149

Open mbcann01 opened 3 months ago

mbcann01 commented 3 months ago

See the note above about the conditional operations quiz. The problem was trying to perform mathematical operations on character strings.

Hi,

I think I know where I messed up in the conditional operations quiz. It all went downhill after task 9.

I had difficulty creating the column for height in feet and then adding it to the column of height in inches so I created a new column where I manually typed the feet in inches but I don't think it correlated to the correct column for height in inches and so all of my numbers were off by 1.

My goal was to subtract 1 from the height_ft and then change it inches and then add to the height_inches column but I had difficulty subtracting 1 and then got an error when I used the $ sign: "non numeric argument to a binary operator" (this is what I tried: class_survey$height_in <- class_survey$height_in /12)

I also tried this but got the same error

              class_survey %>% 

                        mutate(height_ft_new = height_ft - 1)

Can you please help me understand this error and a code to tell R to subtract 1 and then divide by #? This part of the quiz is where I struggled the most. I knew what I wanted but couldn't figure out how to tell R.


For reference- here's what I did...

Task 9. Please create a new column that contains each participant’s overall height in inches. Call this new variable height_overall_in.

class_survey <- class_survey %>%

mutate( height_ft_in = c(72, 72, 72, 72, 72, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 48, 48, NA, NA))

``

``{r}

class_survey$height_in <- as.numeric(class_survey$height_in)

``

``{r}

class_survey <- class_survey %>%

rowwise() %>%

mutate( height_overall_in = sum(height_in, height_ft_in))

``

``{r}

Q4 mean

mean(class_survey$height_overall_in, na.rm = TRUE) %>% round()


I appreciate your time.