Replacing more than one elements with replace function

luciano

In mtcars$am I want to replace instances of 0 with zero and instances of 1 with one. This is what I tried:

library(dplyr)

mtcars %>%
dplyr::select(am) %>%
mutate(am.character = replace(am, am %in% c(0, 1), c("zero", "one"))) %>%
as.data.frame

   am am.character
1   1         zero
2   1          one
3   1         zero
4   0          one
5   0         zero
6   0          one
7   0         zero
8   0          one
9   0         zero
10  0          one
11  0         zero
12  0          one
13  0         zero
14  0          one
15  0         zero
16  0          one
17  0         zero
18  1          one
19  1         zero
20  1          one
21  0         zero
22  0          one
23  0         zero
24  0          one
25  0         zero
26  1          one
27  1         zero
28  1          one
29  1         zero
30  1          one
31  1         zero
32  1          one

But all this has done is created a vector of c(zero, one) that is repeated 16 times. How can I replace instances of 0 with zero and instances of 1 with one?

akrun

You can try by numeric indexing.

 mtcars %>% 
    select(am) %>% 
    mutate(am1= c('zero', 'one')[am+1L])

Or using replace, but this is not useful when there are multiple elements to replace. Best would be to use factor and specify the levels/labels.

 mtcars %>%
      select(am) %>%
      mutate(am1= replace(replace(am, !am, 'zero'), am==1, 'one') )

Or instead of double replace, create a column of zero and replace the zero' byone` based on values of "am"

 mtcars %>%
     select(am) %>% 
     mutate(am1= 'zero', am1=replace(am1, am!=0, 'one'))

Other option where you can change multiple elements with corresponding replacement element is mgsub from qdap

 library(qdap)
  mtcars %>%
        select(am) %>%
        mutate(am1= mgsub(0:1, c('zero', 'one'), am))

Update

If you need to use replace to change the values in one variable based on the other,

mtcars %>% 
       select(am,gear) %>%
       mutate(am= replace(am, gear %in% 4:5, 1000))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replacing more than one elements with replace function

From Dev

More than one function to IF statement

From Dev

Replace strings with more than one character

From Dev

replace more than one pattern python

From Dev

Replace strings with more than one character

From Dev

Javascript - Replace more than one value

From Dev

replace more than one special character with sed

From Dev

CSS specificity for elements with more than one class

From Dev

Android: makeSceneTransition for more than one shared elements

From Dev

Binding More Than One Dynamically Added Elements

From Dev

ddCollapse not working for more than one elements

From Dev

mongodb: replace an element in an array with one or more elements

From Dev

Replace one or more elements in a nested list

From Dev

Use replace() function in an update to change more than one sub string of a column

From Dev

How to remove more than one whitespace from a string in javascript(Can not use regex replace function)?

From Dev

Use replace() function in an update to change more than one sub string of a column

From Dev

insert a function into more than one blueprints in Flask

From Dev

CMake finds more than one main function

From Dev

Return more than one value from a function

From Dev

Folding in Haskell, using more than one function

From Dev

Unpacking more than one list as argument for a function

From Dev

How to disable more than one scroll function?

From Dev

rollapply with function that returns more than one value

From Dev

Integrating more than one function error in python

From Dev

execute function not more than one time

From Dev

more than one function from jquery ui

From Dev

insert a function into more than one blueprints in Flask

From Dev

How to call more than one function in python

From Dev

pass more than one function to a callback

Related Related

  1. 1

    Replacing more than one elements with replace function

  2. 2

    More than one function to IF statement

  3. 3

    Replace strings with more than one character

  4. 4

    replace more than one pattern python

  5. 5

    Replace strings with more than one character

  6. 6

    Javascript - Replace more than one value

  7. 7

    replace more than one special character with sed

  8. 8

    CSS specificity for elements with more than one class

  9. 9

    Android: makeSceneTransition for more than one shared elements

  10. 10

    Binding More Than One Dynamically Added Elements

  11. 11

    ddCollapse not working for more than one elements

  12. 12

    mongodb: replace an element in an array with one or more elements

  13. 13

    Replace one or more elements in a nested list

  14. 14

    Use replace() function in an update to change more than one sub string of a column

  15. 15

    How to remove more than one whitespace from a string in javascript(Can not use regex replace function)?

  16. 16

    Use replace() function in an update to change more than one sub string of a column

  17. 17

    insert a function into more than one blueprints in Flask

  18. 18

    CMake finds more than one main function

  19. 19

    Return more than one value from a function

  20. 20

    Folding in Haskell, using more than one function

  21. 21

    Unpacking more than one list as argument for a function

  22. 22

    How to disable more than one scroll function?

  23. 23

    rollapply with function that returns more than one value

  24. 24

    Integrating more than one function error in python

  25. 25

    execute function not more than one time

  26. 26

    more than one function from jquery ui

  27. 27

    insert a function into more than one blueprints in Flask

  28. 28

    How to call more than one function in python

  29. 29

    pass more than one function to a callback

HotTag

Archive