1

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

library(dplyr)

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

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

3 Answers 3

2
# set as data.table
setDT(df)

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

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

Sign up to request clarification or add additional context in comments.

Comments

2

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))

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.