如何为3组添加一些列值? [英] how to add some value of columns with respect of 3 groups?

查看:69
本文介绍了如何为3组添加一些列值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3列:SAMPN,PERNO,循环。以及与3种模式相对应的实用程序。角豆和步行
我想添加具有相同SAMPN,PERNO,循环的行的实用程序。对于car.car,bus.bus,walk.walk,walk.bus和bus.walk

I have 3 column : SAMPN,PERNO, loop. and utilities corresponding to 3 modes. carobs and walk I want to add utility of rows whose have the same SAMPN,PERNO, loop. for car.car, bus.bus, walk.walk, walk.bus and bus.walk

示例

   SAMPN PERNO  PLANO loop      walk               car               bus            MODE1
  <chr>   <fct> <fct> <fct> <chr>              <chr>             <chr>              <fct>
1 "    4" 1     " 2"  2     -0.990765697239748 2.09989661853416  -0.92177603128108  2    
2 "    4" 1     " 7"  2     0.11385013993979   1.09436996098927  -0.534987482042767 2    
3 "    4" 2     " 2"  2     0.500507525721786  0.924888419124695 -0.376370439308976 2    
4 "    4" 2     " 7"  2     -0.299078042202768 1.54226436622111  -0.289562610169849 2    
5 "    6" 1     " 2"  2     -0.991897610390741 1.58114646818508  -0.973443199067661 2    
6 "    6" 1     " 3"  2     -1.05376527366975  1.61719511863015  -0.832468269682489 2 

在SAMPN 4中,第一行的内容相同SAMPN,PERNO和loop,所以我将添加这两行的交叉实用程序。

in SAMPN 4, 2 first row have same SAMPN , PERNO and loop so I will add the crossponding utility of these 2 rows.

car.car 2.09989661853416+1.09436996098927   = 3.194267
bus.bus -0.92177603128108+-0.534987482042767 =-1.456764
walk.walk -0.990765697239748+0.11385013993979=-0.8769156
walk.bus -0.92177603128108+-0.534987482042767=-1.456764
bus.walk -0.92177603128108+ 0.11385013993979 =-0.8079259

与其他人相同。

我知道是否要在每行中添加值我可以使用它:但是如何在不同的行中添加实用工具?

I know if I wanted to add the value in each row I could use this: but how to add utility in different rows?

kl<-r %>%
+   group_by(SAMPN, PERNO,loop) %>%
+   mutate(car.car = car+car, walk.walk=walk+walk, bus.bus=bus+bus, walk.bus=walk+bus, bus.walk=bus+walk)

数据:

structure(list(SAMPN = c("    4", "    4", "    4", "    4", 
"    6", "    6"), PERNO = structure(c(1L, 1L, 2L, 2L, 1L, 1L
), .Label = c("1", "2", "3", "4", "5", "6", "7"), class = "factor"), 
    PLANO = structure(c(1L, 6L, 1L, 6L, 1L, 2L), .Label = c(" 2", 
    " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10", "11", "12", 
    "13", "14", "15", "16", "17", "18", "19", "20", "21", "23", 
    "24"), class = "factor"), loop = structure(c(2L, 2L, 2L, 
    2L, 2L, 2L), .Label = c("1", "2", "3", "4", "5", "6", "7", 
    "8"), class = "factor"), walk = c("-0.990765697239748", "0.11385013993979", 
    "0.500507525721786", "-0.299078042202768", "-0.991897610390741", 
    "-1.05376527366975"), car = c("2.09989661853416", "1.09436996098927", 
    "0.924888419124695", "1.54226436622111", "1.58114646818508", 
    "1.61719511863015"), bus = c("-0.92177603128108", "-0.534987482042767", 
    "-0.376370439308976", "-0.289562610169849", "-0.973443199067661", 
    "-0.832468269682489"), MODE1 = structure(c(2L, 2L, 2L, 2L, 
    2L, 2L), .Label = c("1", "2", "3", "4"), class = "factor")), row.names = c(NA, 
-6L), groups = structure(list(SAMPN = c("    4", "    4", "    6"
), PERNO = structure(c(1L, 2L, 1L), .Label = c("1", "2", "3", 
"4", "5", "6", "7"), class = "factor"), loop = structure(c(2L, 
2L, 2L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8"), class = "factor"), 
    .rows = list(1:2, 3:4, 5:6)), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

仅输出前两行:

 SAMPN PERNO  PLANO loop      car.car      bus.bus  walk.walk    walk.bus  bus.walk  MODE1

1 "    4" 1     " 2"  2    3.194267    -1.456764    -0.8769156  -1.456764  -0.8079259   2
2 "    4" 1     " 7"  2    3.194267    -1.456764    -0.8769156  -1.456764  -0.8079259   2


推荐答案

如果需要为特定组合创建,在 group_by 步骤之后,可以一个一列地创建列

If we need to create for specific combinations, after the group_by step, can create the columns one by one

library(dplyr)
df1 %>% 
   group_by(SAMPN, PERNO, loop) %>%
   mutate_at(vars(walk:bus), as.numeric) %>%
   mutate(walk.bus = first(walk) + last(bus), 
           bus.walk = first(bus) + last(walk), 
           walk.walk = sum(walk),
           bus.bus = sum(bus), 
           car.car = sum(car))

这篇关于如何为3组添加一些列值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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