如何使用摘要将新计算分组?

我想按y列对数据进行分组。我需要对每个值y求和的差异进行额外的计算。 函数summarise是否适用于这种类型的计算?

x <- sample(c(0:5),20,replace = T)
y <- rep(c("A","B","C","D"),each = 5)
df <- data.frame(y,x)

inds <- c(0,diff(df$x))
inds <- -inds * (inds < 0)

df %>% 
  group_by(y) %>% 
  summarise(inds <- c(0,diff(df$x)),inds <- -inds * (inds < 0))

> inds
[1] 0 2 0 0 5 0 0 1 2
> cbind(df,inds)
  y x inds
1 A 3    0
2 A 1    2
3 A 5    0
4 B 5    0
5 B 0    5
6 B 1    0
7 C 3    0
8 C 2    1
9 C 0    2

Desired outcome:
y x new_variable
1 A 2    
2 B 5    
3 C 3    

实际上我得到了错误:

  

错误:列inds

hoyob 回答:如何使用摘要将新计算分组?

我想你想做的是

library(dplyr)

df %>%
  mutate(inds = c(0,diff(x)),inds = -inds * (inds < 0)) %>%
  group_by(y) %>%
  summarise(sum = sum(inds))

#   y       sum
#  <fct> <dbl>
#1 A         2
#2 B         5
#3 C         3

数据

df <- structure(list(y = structure(c(1L,1L,2L,3L,3L),.Label = c("A","B","C"),class = "factor"),x = c(3L,5L,0L,0L)),row.names = c("1","2","3","4","5","6","7","8","9"),class = "data.frame")
,

global log 127.0.0.1 local0 notice maxconn 2000 user haproxy group haproxy defaults log global mode http option httplog option dontlognull option forwardfor option http-server-close retries 3 option redispatch timeout connect 5000 timeout client 10000 timeout server 10000 frontend ELB bind *:80 mode http http-response set-header Strict-Transport-Security "max-age=16000000; includeSubDomains; preload;" stats uri /haproxy?stats stats auth hapuser:hapuser acl apicall path_beg -i /api/ use_backend apigateway if apicall acl explorercall path_beg -i /explorer/ use_backend apigateway if explorercall default_backend weburl backend weburl mode http balance leastconn compression algo gzip compression type text/css text/html text/javascript application/javascript text/plain text/xml application/json image/svg+xml http-response set-header Strict-Transport-Security "max-age=16000000; includeSubDomains; preload;" server weburl1 127.0.0.1:3000 check port 80 inter 10s rise 2 fall 2 backend apigateway mode http balance leastconn compression algo gzip compression type text/css text/html text/javascript application/javascript text/plain text/xml application/json image/svg+xml http-response set-header Strict-Transport-Security "max-age=16000000; includeSubDomains; preload;" server apigateway1 127.0.0.1:6001 check port 80 inter 10s rise 2 fall 2 内使用sum,而无需使用$,并在%>%中使用set.seed,以确保可重复性。

sample
,

您可以先使用 Integer total = 1; double porcion = 3.5; //every single pizza double bebida = 2.0; //the drink double totalAmount = (total * porcion) + bebida; String totalAmount2 = String.valueOf(totalAmount); // This method increases the amount of pizzas botonmas.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { total++; tv1.setText(total.toString()); dinero.setText(totalAmount2); } }); // This method decreases the amount of pizzas botonmenos.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(total > 1){ total--; tv1.setText(total.toString()); dinero.setText(totalAmount2); } } }); 创建mutate列,然后再使用inds

summarise
,

您也可以在基数R中执行此操作。

aggregate(. ~ y,transform(DF,ind=-c(0,diff(DF$x)) * (c(0,diff(DF$x)) < 0)),sum)
#   y x ind
# 1 A 9   2
# 2 B 6   5
# 3 C 5   3

数据

DF <- structure(list(y = c("A","A","C",class = "data.frame")
本文链接:https://www.f2er.com/3146566.html

大家都在问