用替换功能替换多个元素 [英] Replacing more than one elements with replace function

查看:315
本文介绍了用替换功能替换多个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mtcars $ am 我想用0替换0的实例,将一个实例替换为1。这是我试过的:

  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零
2 1一
3 1零
4 0一
5 0零
6 0一
7 0零
8 0一
9 0零
10 0一
11 0零
12 0一
13 0零
14 0一
15 0零
16 0一
17 0零
18 1一
19 1零
20 1一
21 0零
22 0一
23 0零
24 0一
25 0零b $ b 26 1一
27 1零
28 1一
29 1零
30 1一
31 1零
3 2 1一个

但是这一切都是创建一个c(零,一)的向量,重复16次。如何用0替换0的实例,将一个实例替换为1?

解决方案

您可以尝试数字索引。

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

或使用替换,但是当有多个要替换的元素时,这不是有用的。最好是使用因子并指定级别/标签



($)$($)$ m $%
select(am)%>%
mutate(am1 = ),am == 1,'one'))

或代替双重替换,创建一列替换 零'由一个基于am的值

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

其他可以用相应的替换元素更改多个元素的选项是 mgsub qdap

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



更新



如果您需要使用替换根据其他变量更改一个变量中的值

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


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?

解决方案

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

这篇关于用替换功能替换多个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆