Home > database >  If variable meets condition, replace value of another variable R
If variable meets condition, replace value of another variable R

Time:02-05

I've been looking at the various answers for similar issues, but can't see anything that quite answers my problem.

I have a large data table

| Number_X | Amount | | ------- | -------| | 1 | 100 | | 2 | 100 | | 1 | 100 | | 3 | 100 | | 1 | 100 | | 2 | 100 | I want to replace the amount with 50 for those rows where Number_X == 1. I've tried

data <- data %>%
  mutate(Amount = replace(Amount, Number_X == 1, 50))

but it doesn't change the value for Amount. How can I fix this?

CodePudding user response:

# set as data.table
setDT(df)

# if then
df[ Number_X == 1, Amount := 50]

With large data, a data.table solution is most appropriate.

CodePudding user response:

I don't see an issue with using replace() but you can also try to use if_else()

library(dplyr, warn.conflicts = FALSE)

data <- tibble(
  Number_X = c(1L, 2L, 1L, 3L, 1L, 2L),
  Amount = c(100L, 100L, 100L, 100L, 100L, 100L)
)

data %>% 
  mutate(Amount = replace(Amount, Number_X == 1, 50L))
#> # A tibble: 6 x 2
#>   Number_X Amount
#>      <int>  <int>
#> 1        1     50
#> 2        2    100
#> 3        1     50
#> 4        3    100
#> 5        1     50
#> 6        2    100

data %>% 
  mutate(Amount = if_else(Number_X == 1, 50L, Amount))
#> # A tibble: 6 x 2
#>   Number_X Amount
#>      <int>  <int>
#> 1        1     50
#> 2        2    100
#> 3        1     50
#> 4        3    100
#> 5        1     50
#> 6        2    100

Created on 2022-02-04 by the reprex package (v2.0.1)

Tip: Use dput() with your data to share it more easily:

dput(data)
#> structure(list(Number_X = c(1L, 2L, 1L, 3L, 1L, 2L), Amount = c(100L, 
#> 100L, 100L, 100L, 100L, 100L)), class = c("tbl_df", "tbl", "data.frame"
#> ), row.names = c(NA, -6L))

CodePudding user response:

If you want a tidyverse approach:

data %>%
    mutate(Amount = ifelse(Number_X == 1, 50, Amount))

If you want almost the speed of data.table and the grammar of dplyr, you can consider dtplyr.

  •  Tags:  
  • Related