有条件的For循环的数据帧中每列的总和 [英] Sum Values of Every Column in Data Frame with Conditional For Loop

查看:53
本文介绍了有条件的For循环的数据帧中每列的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想遍历数据集并根据第一列的条件求和每一列的值.到目前为止的数据和我的代码如下:

So I want to go through a data set and sum the values from each column based on the condition of my first column. The data and my code so far looks like this:

x    v1    v2    v3
1    0     1     5
2    4     2     10 
3    5     3     15
4    1     4     20

for(i in colnames(data)){
    if(data$x>2){
        x1 <-sum(data[[i]])
        }
    else{
        x2 <-sum(data[[i]])
        }
      }

我的假设是for循环将按名称从数据中调用每一列,然后根据它们是否符合x列的条件对每列中的值求和.

My assumption was that the for loop would call each column by name from the data and then sum the values in each column based on whether they matched the condition of column x.

我想将每一列的值相加一半,并将它们分配给值x1,其余部分也做同样的事情,将其分配给x2.我不断收到以下错误消息:

I want to sum half the values from each column and assign them to a value x1 and do the same for the remainder, assigning it to x2. I keep getting an error saying the following:

the condition has length > 1 and only the first element will be used

我做错了什么,还有更好的方法解决吗?理想情况下,我想要一个看起来像这样的表:

What am I doing wrong and is there a better way to go about this? Ideally I want a table that looks like this:

       v1    v2    v3
x1     6     7     35
x2     4     3     15

推荐答案

这是一个 dplyr 解决方案.首先,我定义数据框.

Here's a dplyr solution. First, I define the data frame.

df <- read.table(text = "x    v1    v2    v3
1    0     1     5
2    4     2     10 
3    5     3     15
4    1     4     20", header = TRUE)  

#   x v1 v2 v3
# 1 1  0  1  5
# 2 2  4  2 10
# 3 3  5  3 15
# 4 4  1  4 20

然后,我创建一个标签( x_check )以根据您的条件( x> 2 )来指示每行属于哪个组,并按此标签进行分组,并使用 sum 将每个列的名称总结为 v .

Then, I create a label (x_check) to indicate which group each row belongs to based on your criterion (x > 2), group by this label, and summarise each column with a v in its name using sum.

# Load library
library(dplyr)

df %>% 
  mutate(x_check = ifelse(x>2, "x1", "x2")) %>% 
  group_by(x_check) %>% 
  summarise_at(vars(contains("v")), funs(sum))

# # A tibble: 2 x 4
#   x_check    v1    v2    v3
#   <chr>   <int> <int> <int>
# 1 x1          6     7    35
# 2 x2          4     3    15

这篇关于有条件的For循环的数据帧中每列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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