H, 我有一个这样的数据框:
d <- data.frame(v1=seq(0,9.9,0.1),
v2=rnorm(100),
v3=rnorm(100))
> head(d)
v1 v2 v3
1 0.0 -0.01431916 -0.5005415
2 0.1 -1.01575590 1.5307473
3 0.2 1.00081065 -0.1730830
4 0.3 -1.20697918 0.5105118
5 0.4 -2.16698578 -1.0120544
6 0.5 0.33886508 0.4797016
我现在想要一个新的数据框,它总结了0-0.99,1-1.99,2-2.99,3-3.99 ......之间的所有值,例如
像这样start end mean.v2 mean.v3
0 1 0.2 0.1
1 2 0.5 0.4
等等
感谢
更新我应该在我的实际数据集中添加,每个区间的观察结果具有不同的长度,它们并不总是从零开始或以10结束
答案 0 :(得分:4)
这是@akrun建议使用cut()
的一种方式:
d %>% mutate( ints = cut(v1 ,breaks = 11)) %>%
group_by(ints) %>%
summarise( mean.v2 = mean(v2) , mean.v3 = mean(v3) )
答案 1 :(得分:3)
使用floor()和ceiling()函数。例如,在间隔为1 - 1或2 - 2的情况下,ifelse()。
d<-data.frame(v1=seq(0,9.9,0.1),
v2=rnorm(100),
v3=rnorm(100))
library(dplyr)
d%>%
mutate(start=floor(v1),
end=ifelse(ceiling(v1)==start,start+1,ceiling(v1)))%>%
group_by(start,end)%>%
summarise(mean.v2=mean(v2),
mean.v3=mean(v3))
Source: local data frame [10 x 4]
Groups: start [?]
start end mean.v2 mean.v3
(dbl) (dbl) (dbl) (dbl)
1 0 1 0.135180183 -0.36083298
2 1 2 -0.245567899 0.26827020
3 2 3 -0.051136441 0.14211666
4 3 4 0.252451303 0.38530797
5 4 5 0.007209073 0.30137345
6 5 6 -0.307008690 0.07662942
7 6 7 0.103271270 0.14734865
8 7 8 0.016753997 -0.02559756
9 8 9 -0.199958098 -0.21821830
10 9 10 0.532339512 -0.46509108
相同但包括名为interval而不是two(start和end)的列:
d%>%
mutate(start=floor(v1),
end=ifelse(ceiling(v1)==start,start+1,ceiling(v1)),
interval=paste(start,"-",end))%>%
select(-start,-end)%>%
group_by(interval)%>%
summarise(mean.v2=mean(v2),
mean.v3=mean(v3))
Source: local data frame [10 x 3]
interval mean.v2 mean.v3
(chr) (dbl) (dbl)
1 0 - 1 0.135180183 -0.36083298
2 1 - 2 -0.245567899 0.26827020
3 2 - 3 -0.051136441 0.14211666
4 3 - 4 0.252451303 0.38530797
5 4 - 5 0.007209073 0.30137345
6 5 - 6 -0.307008690 0.07662942
7 6 - 7 0.103271270 0.14734865
8 7 - 8 0.016753997 -0.02559756
9 8 - 9 -0.199958098 -0.21821830
10 9 - 10 0.532339512 -0.46509108
答案 2 :(得分:2)
基于@David H&#34的答案,有2个选项可供选择:
floor()
的间隔cut()
代替set.seed(33)
d <- data.frame(v1=seq(0,9.9,0.1),
v2=rnorm(100),
v3=rnorm(100))
创建数据
cut()
breaks <- 0:10
的间隔
对于这个简单的示例,您可以使用d$v1
,但为了更加通用,请使用breaks <- floor(min(d$v1)):ceiling(max(d$v1))
breaks
# [1] 0 1 2 3 4 5 6 7 8 9 10
的最小值和最大值。
d %>%
mutate(interval = cut(v1,
breaks,
include.lowest = TRUE,
right = FALSE)) %>%
group_by(interval) %>%
summarise( mean.v2 = mean(v2) , mean.v3 = mean(v3))
# Source: local data frame [10 x 3]
#
# interval mean.v2 mean.v3
# (fctr) (dbl) (dbl)
# 1 [0,1) -0.13040624 -0.20781247
# 2 [1,2) 0.26505794 0.51990167
# 3 [2,3) 0.13451628 1.12066174
# 4 [3,4) 0.23451272 -0.14773437
# 5 [4,5) 0.34326922 0.28567969
# 6 [5,6) -0.77059944 -0.16629580
# 7 [6,7) -0.17617190 0.03320797
# 8 [7,8) 0.86550135 -0.24664350
# 9 [8,9) -0.06652047 -0.27798769
# 10 [9,10] -0.10424865 0.24060163
总结时间间隔0-0.99,1-1.99,2-2.99,3-3.99,......
floor()
cut()
代替1e-9
通过从每个间隔的末尾减去一个微小的数字d %>%
mutate(start = floor(v1), end = start + 1 - 1e-9 ) %>%
group_by(start, end) %>%
summarise_each(funs(mean))
# Source: local data frame [10 x 4]
# Groups: start [?]
#
# start end mean.v2 mean.v3
# (dbl) (dbl) (dbl) (dbl)
# 1 0 1 -0.13040624 -0.20781247
# 2 1 2 0.26505794 0.51990167
# 3 2 3 0.13451628 1.12066174
# 4 3 4 0.23451272 -0.14773437
# 5 4 5 0.34326922 0.28567969
# 6 5 6 -0.77059944 -0.16629580
# 7 6 7 -0.17617190 0.03320797
# 8 7 8 0.86550135 -0.24664350
# 9 8 9 -0.06652047 -0.27798769
# 10 9 10 -0.10424865 0.24060163
来作弊。
knex.schema.table(tableName, function(table) {
table.string('test').catch(function(e) {
callback(e)
});
}).then(function(e) {
callback(e);
}).catch(function(e) {
callback(e);
})