如何停止在 dplyr 中使用 rowwise? [英] How does one stop using rowwise in dplyr?

查看:12
本文介绍了如何停止在 dplyr 中使用 rowwise?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,如果希望在 dplyr 中逐行应用操作,可以使用 rowwise 函数,例如:使用 dplyr 将函数应用于表的每一行?

So, if one wishes to apply an operation row by row in dplyr, one can use the rowwise function, for example: Applying a function to every row of a table using dplyr?

是否有一个 unrowwise 函数可以用来逐行停止操作?目前,似乎在 rowwise 删除行操作之后添加了一个 group_by,例如

Is there a unrowwise function which you can use to stop doing operations row by row? Currently, it seems adding a group_by after the rowwise removes row operations, e.g.

data.frame(a=1:4) %>% rowwise() %>% group_by(a)
# ...
# Warning message:
# Grouping rowwise data frame strips rowwise nature 

这是否意味着如果您希望显式删除 rowwise,应该使用 group_by(1)?

Does this mean one should use group_by(1) if you wish to explicitly remove rowwise?

推荐答案

正如在评论和其他答案中发现的那样,正确的做法是使用 ungroup().

As found in the comments and the other answer, the correct way of doing this is to use ungroup().

操作rowwise(df)df 的类之一设置为rowwise_df.通过检查代码here,给出以下ungroup方法:

The operation rowwise(df) sets one of the classes of df to be rowwise_df. We can see the methods on this class by examining the code here, which gives the following ungroup method:

#' @export
ungroup.rowwise_df <- function(x) {
  class(x) <- c( "tbl_df", "data.frame")
  x
}

所以我们看到ungroup 并没有严格删除分组结构,而是删除了从rowwise 函数中添加的rowwise_df 类.

So we see that ungroup is not strictly removing a grouped structure, instead it just removes the rowwise_df class added from the rowwise function.

这篇关于如何停止在 dplyr 中使用 rowwise?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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