向data.frame添加行,这些行是从现有行计算的

时间:2017-11-28 15:20:55

标签: r

给出

$response.RawContent.Split("`n")[0..20]
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Origin, Accept-Encoding
Access-Control-Allow-Credentials: true
Pragma: no-cache
X-Content-Type-Options: nosniff
CF-Cache-Status: HIT
CF-RAY: 3c4e3f804f9d82f7-ATL
Cache-Control: public, max-age=14400
Content-Type: application/json; charset=utf-8
Date: Tue, 28 Nov 2017 15:09:23 GMT
Expires: Tue, 28 Nov 2017 19:09:23 GMT
ETag: W/"6b80-Ybsq/K6GwwqrYkAsFxqDXGC7DoM"
Set-Cookie: __cfduid=d84018de2d621df9d53eb52d97cd33a651511881763; expires=Wed, 28-Nov-18 15:09:23 GMT; path=/; domain=.typicode.com; HttpOnly
Server: cloudflare-nginx
Via: 1.1 vegur
X-Powered-By: Express

有没有办法按x = data.frame(value = 1:3, col2 = c('min', 'min', 'max'), id = c(1, 2, 1)) 分组,然后平均col3min然后将结果存储在max

col1

2 个答案:

答案 0 :(得分:1)

如果您只有这三列,并且每组只有2个值(最小和最大),那么,

library(dplyr)

x %>% 
 group_by(col3) %>% 
 summarise(col1 = mean(col1), col2 = 'Average') %>% 
 bind_rows(x) %>% 
 arrange(col3)

给出,

# A tibble: 5 x 3
   col3  col1    col2
  <dbl> <dbl>   <chr>
1     1     2 Average
2     1     1     min
3     1     3     max
4     2     2 Average
5     2     2     min

答案 1 :(得分:0)

我认为你正在寻找这个:

library(dplyr)
x = data.frame(value = 1:3, col2 = c('min', 'min', 'max'), id = c(1, 2, 1))
x %>% group_by(id) %>% summarize(avg=mean(value)

这会为您提供所需的值,但不会将它们附加到您的数据框中。

在这几个背景下,我不确定你应该这样做,但如果你真的想,你可以rbind