mutate rowSums排除一列 [英] mutate rowSums exclude one column

查看:143
本文介绍了mutate rowSums排除一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据框架

> df
Source: local data frame [4 x 4]

      a x y z
1 name1 1 1 1
2 name2 1 1 1
3 name3 1 1 1
4 name4 1 1 1

想要通过添加列x,y和z(可以有更多的数字列)。尝试排除列'a'如下所示不起作用。

Want to mutate it by adding columns x, y, and z (there can be many more numeric columns). Trying to exclude column 'a' as follows is not working.

dft <- df %>% mutate(funs(total = rowSums(.)), -a)
Error: not compatible with STRSXP

这也会产生错误:

dft <- df %>% mutate(total = rowSums(.), -a)
Error in rowSums(.) : 'x' must be numeric

什么是

推荐答案

如果要在结果中保留非数字列,可以执行以下操作:

If you want to keep non-numeric columns in the result, you can do this:

dat %>% mutate(total=rowSums(.[, sapply(., is.numeric)]))

这篇关于mutate rowSums排除一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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