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

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

问题描述

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



是否有向右移动函数,可用于停止逐行执行操作?当前,似乎在行方向删除行操作之后添加 group_by ,例如

  data.frame(a = 1:4)%&%;%rowwise()%&%;%group_by(a)
#...
#警告消息:
#对行数据框进行分组将行性质

这是否意味着如果您想显式删除 rowwise


<,应该使用 group_by(1) div class = h2_lin>解决方案

在注释和其他答案中都可以找到,正确的方法是使用 ungroup()



操作 rowwise(df)设置 df rowwise_df 。我们可以通过检查代码此处,它给出以下 ungroup 方法:

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

所以我们看到 ungroup 并非严格删除分组的结构,而只是删除从 rowwise 添加的 rowwise_df 类功能。


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?

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 

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

解决方案

As found in the comments and the other answer, the correct way of doing this is to use 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
}

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中使用按行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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