dplyr变异到位 [英] dplyr mutate in place
本文介绍了dplyr变异到位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的例子
library('dplyr')
a < - c(0.2,1.3)
df_test< - data.frame(a)
df_test%>%mutate(a = round(a,0))
它产生:
a
1 0
2 1
,但不会更改原始数据框 df_test
。如何将mutate的结果分配给同一个数据框?
解决方案
我们可以使用%< >%
复制赋值运算符从 magrittr
更改位置
df_test%<>%
mutate(a = round(a,0))
p $ p>
如果我们使用
data.table
:=
)操作符也没有复制library data.table)
setDT(df_test)[,a:= round(a,0)]
Here is my example
library('dplyr') a <- c(0.2,1.3) df_test <- data.frame(a) df_test %>% mutate(a =round(a,0))
it produces:
a 1 0 2 1
, but does not change original dataframe
df_test
. How do I assign results of mutate to the same dataframe?解决方案We can use the
%<>%
compound assignment operator frommagrittr
to change in placelibrary(magrittr) df_test %<>% mutate(a = round(a,0))
If we are using
data.table
, the assignment (:=
) operator does this in place too without copyinglibrary(data.table) setDT(df_test)[, a := round(a,0)]
这篇关于dplyr变异到位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文