Recode和Mutate_all在dplyr [英] Recode and Mutate_all in dplyr

查看:157
本文介绍了Recode和Mutate_all在dplyr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用recode和mutate_all来重新编列列。但是,由于某些原因,我收到错误。我相信这篇文章类似于如何在dplyr列中重新编码(和反向代码)变量,但该帖子中的答案已经使用了lapply函数。



这是我在阅读dplyr包的帮助之后尝试的pdf。

  by_species< -matrix(c(1,2,3,4),2,2)
tbl_species< -as_data_frame(by_species)
tbl_species%>%mutate_all(funs(。* 0.4))
#A tibble:2 x 2
V1 V2
< dbl& < DBL>
1 0.4 1.2
2 0.8 1.6

所以,这个效果很好。



但是,这不起作用:

 矩阵(c(A,A  - ,B,C,D,B  - ,C,C,F),3,3)
tbl_grades< - as_data_frame(grade)
tbl_grades%>%mutate_all(funs(dplyr :: recode(。,A ='4.0')))
pre>

我收到这个错误:

  vapply $($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 

有人可以解释问题是什么,为什么上面的代码不起作用?



感谢任何帮助。



谢谢

解决方案

@Mir做了一个很好的工作来描述问题。这是一个可能的解决方法。由于问题在于生成名称,您可以提供自己的名称

  tbl_grades%>%mutate_all(funs(recode =重新编码(。,A ='4.0')))

现在添加列而不是替换它们。这是一个功能,将忘记您提供这些名称

  dropnames< -function(x){if(is(x ,lazy_dots)){attr(x,has_names)< -FALSE}; x} 
tbl_grades%>%mutate_all(dropnames(funs(recode = dplyr :: recode(。,A ='4.0'))))

这样做应该是原来的。虽然真的

  tbl_grades%>%mutate_all(dropnames(funs(recode(。,A ='4.0')))) 

因为dplyr通常有一些函数的特殊c ++版本,如果它识别了这些函数,就可以使用例如, lag ),但如果您还指定命名空间(如果使用 dplyr :: lag ),则不会发生。


I am trying to use recode and mutate_all to recode columns. However, for some reason, I am getting an error. I do believe this post is similar to how to recode (and reverse code) variables in columns with dplyr but the answer in that post has used lapply function.

Here's what I tried after reading dplyr package's help pdf.

by_species<-matrix(c(1,2,3,4),2,2)
tbl_species<-as_data_frame(by_species)
tbl_species %>% mutate_all(funs(. * 0.4))
# A tibble: 2 x 2
     V1    V2
  <dbl> <dbl>
1   0.4   1.2
2   0.8   1.6

So, this works well.

However, this doesn't work:

grades<-matrix(c("A","A-","B","C","D","B-","C","C","F"),3,3)
tbl_grades <- as_data_frame(grades)
tbl_grades %>% mutate_all(funs(dplyr::recode(.,A = '4.0')))

I get this error:

    Error in vapply(dots[missing_names], function(x) make_name(x$expr), character(1)) : 
values must be length 1,
but FUN(X[[1]]) result is length 3

Can someone please explain what's the problem and why above code isn't working?

I'd appreciate any help.

Thanks

解决方案

@Mir has done a good job describing the problem. Here's one possible workaround. Since the problem is in generating the name, you can supply your own name

tbl_grades %>% mutate_all(funs(recode=recode(.,A = '4.0')))

Now this does add columns rather than replace them. Here's a function that will "forget" that you supplied those names

dropnames<-function(x) {if(is(x,"lazy_dots")) {attr(x,"has_names")<-FALSE}; x}
tbl_grades %>% mutate_all(dropnames(funs(recode=dplyr::recode(.,A = '4.0'))))

This should behave like the original. Although really

tbl_grades %>% mutate_all(dropnames(funs(recode(.,A = '4.0'))))

because dplyr often has special c++ versions of some functions that it can use if it recognized the functions (like lag for example) but this will not happen if you also specify the namespace (if you use dplyr::lag).

这篇关于Recode和Mutate_all在dplyr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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