-2

Having this dataframe:

dat=data.frame(a=c("ll","pp","ml","ml","v"),value=c(1,2,12,1,2))

I want to multiply by 10 only values correspond to a=ml

bic ton
  • 1,117
  • 1
  • 7
  • 14

2 Answers2

2

In base R:

dat=data.frame(a=c("ll","pp","ml","ml","v"),value=c(1,2,12,1,2))

dat$value[dat$a=="ml"] = dat$value[dat$a=="ml"] * 10
dat

Output:

   a value
1 ll     1
2 pp     2
3 ml   120
4 ml    10
5  v     2
SamR
  • 1,790
  • 4
  • 14
1

Another solution is to use a ifelse statement

dat %>% 
  mutate(value = ifelse(a == "ml", value*10, value))

   a value
1 ll     1
2 pp     2
3 ml   120
4 ml    10
5  v     2
Maël
  • 15,428
  • 3
  • 14
  • 43
  • 4
    Please search existing questions before posting answers. If answer is too easy, chances are it has been asked before. – zx8754 Jan 18 '22 at 12:47