0

Hello stackoverflow Community,

I have the following problem in the data set. There are values ​​in the data set that are incorrect e.g. 2.5 where the value should be 20.5. So I multiply the values ​​that are less than 10 by 10. But the result is not correct. For example, in 2007 I get the value 666 for Indonesia instead of 20.5.

library(mosaic)

path <- "/Users/Admin/Desktop/LifeExpectancyData.csv"
df_lifeExpectancy <- read.csv(path)

df_lifeExpectancy1 <- df_lifeExpectancy %>% 

                    select(Country, Year, Status, Life.expectancy, BMI) %>%
                    rename("year" = "Year", 
                           "status" = "Status",
                           "life_expectancy" = "Life.expectancy",
                           "country" = "Country",
                    )

df_lifeExpectancy1 <- na.omit(df_lifeExpectancy1)
df_lifeExpectancy1$BMI[df_lifeExpectancy1$BMI < 10] <- df_lifeExpectancy1$BMI * 10.0

Problem:

Without data manipulation With data manipulation

Thank you for helping.

tanaytuncer
  • 29
  • 1
  • 4
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Pictures of data are not helpful because we cannot copy/paste those values into R for testing. – MrFlick Dec 21 '20 at 08:31
  • But I think what you want is `df_lifeExpectancy1$BMI[df_lifeExpectancy1$BMI < 10] <- df_lifeExpectancy1$BMI[df_lifeExpectancy1$BMI < 10] * 10.0`. You need to subset the same values on both sides of the assignment. – MrFlick Dec 21 '20 at 08:31
  • Thank you for helping MrFlick. You are right I must subset the data. And the next time I will provide a reproducible example. – tanaytuncer Dec 21 '20 at 09:08

1 Answers1

0

You need to subset the data from both the end.

inds <- df_lifeExpectancy1$BMI < 10
df_lifeExpectancy1$BMI[inds] <- df_lifeExpectancy1$BMI[inds] * 10
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213