Matt
Matt

Reputation: 29

converting kilograms to pounds using coded values in R with values under the same variable

I am requesting help to see the best way to convert kilograms to pounds in R using coded values.

For example,

The values in the variable for pounds are denoted 0-999 and the values for kilograms are denoted 9000-9998 where the first number of "9" denotes that it is in kilograms.

I am struggling to convert the kilogram coded values into pounds.

so far I was thinking of using the mutate function with the ifelse function but cannot figure out how I should convert it.

The current attempted thought process is shown below.

mutate(WEIGHT = ifelse(WEIGHT == 9000:9998,...,...))

Any help would be much appreciated.

Upvotes: 0

Views: 570

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226871

How about

data %>% mutate(across(WEIGHT, ~ifelse(. < 1000, ., (. - 9000) * 2.2)) ?

(if the numeric value is less than 1000 assume that it represents a weight in pounds and, leave it alone; otherwise, subtract 9000 (to get the weight in kg) and multiply by 2.2 to convert to pounds)

At least, that's how I'm interpreting your coding.

Upvotes: 1

Related Questions