Q: Perform mathematical operation on column values exceeding specific condition in R Data cleaning question: I have a column in a dataframe that has survey responses to weight in both pounds and kilograms. I need to convert all of the kilogram values of the column to pounds and still keep my dataframe. The kilogram values are preceded by a 9 (they are coded as follows: for 95kg, the number is 9095). So, basically, I have to subtract 9000 from each value and multiply it by 2.20462. I am comparing people in a large dataset by whether or not they have arthritis. Here is the problem I am trying to solve and then the code:
Q3: Compare only those who have and have not had some form of arthritis, rheumatoid arthritis, gout, etc. For those groupings, convert reported weight in kilograms to pounds.  Then, compute the mean and standard deviation of the newly created weight in pounds variable. Use the conversion 1KG = 2.20462 LBS. Make sure the units are in pounds, not two decimals implied. The names of the variables should be mean_weight and sd_weight. mean_weight should equal 183.04. The output for this should be a tibble/dataframe/table with two rows (one for the haves and one for the have-nots) and two columns (mean_weight and sd_weight). So four values all together:
mean_weight  sd_weight
183.04       xx.xx
xxx.xx       xx.xx

Okay. The code so far:
 (library(tidyverse))

 (library(lm.beta))

 brfss <- read.csv("BRFSS2015.csv")

 arthritis <- brfss %>%
   select(HAVARTH3, WEIGHT2) %>%
   filter(HAVARTH3 == '1') %>%
   filter(WEIGHT2 != 7777) %>%
   filter(WEIGHT2 != 9999) 

 kg2lb1 <- ifelse(arthritis$WEIGHT2 > 9000, arthritis$WEIGHT2 - 9000*2.20462, 
 arthritis$WEIGHT2)

 no_arthritis <- brfss %>%
   select(HAVARTH3, WEIGHT2) %>%
   filter(HAVARTH3 == '2') %>%
   filter(WEIGHT2 != 7777) %>%
   filter(WEIGHT2 != 9999)

 kg2lb2 <- ifelse(no_arthritis$WEIGHT2 > 9000, no_arthritis$WEIGHT2 - 9000*2.20462, 
 no_arthritis$WEIGHT2)

When I use the ifelse function, it converts it into a huge numeric list. I need to retain the data frame format without writing a whole bunch more code.

A: Write a conversion function,
kg_to_lb <- \(x) x - 9000*2.20462

apply it in transform to add a new variable with converted values
mtcars <- transform(mtcars, mpg_lb=kg_to_lb(mpg))

and aggregate it by a group (here I use the binary am group of the mtcars data set that comes with R) and applying a FUNction.
aggregate(mpg_lb ~ am, mtcars, FUN=\(x) c(mean=mean(x), sd=sd(x)))
#   am   mpg_lb.mean     mpg_lb.sd
# 1  0 -19824.432632      3.833966
# 2  1 -19817.187692      6.166504

