这是在两个级别使用R中的聚合函数。 对于以下数据框,
df <- data.frame( Event = c('A1','A1','A1','A1','A1'),
Time = c(10,11,21,17,12),
Type = c('New','New','Repeat','Repeat','Repeat'))
在某个级别,以下是完美的工作
aggregate(df$Time,
by=list(df$Event),
FUN = function(x) c(mintime = min(x),endtime = max(x)))
我正在寻找带有条件检查的每个Type的计数。
## CODE NOT WORKING
aggregate(df,by=list(df$Event),
FUN = function(x) c(mintime = min(df$time),endtime = max(df$time), New=length(df$time=='New'),Repeat=length(df$time=='Repeat')))
所以我会得到
Group.1 x.mintime x.maxtime x.New x.Repeat
A1 10 21 2 3
答案 0 :(得分:2)
使用server.contextPath=/refms
:
localhost:8080/refms/api/account/authenticate
使用localhost:8080/refms-xxxx/api/account/authenticate
:
data.table
<强>结果:强>
library(data.table)
setDT(df)[, .(mintime = min(Time),
maxtime = max(Time),
New = sum(Type=="New"),
Repeat = sum(Type=="Repeat")),
by = Event]
答案 1 :(得分:1)
使用来自dplyr的 group_by()和 summary(),这非常容易。请参阅以下内容:
library(dplyr)
df <- data.frame( Event = c('A1','A1','A1','A1','A1'),
Time = c(10,11,21,17,12),
Type = c('New','New','Repeat','Repeat','Repeat'))
df %>%
group_by(Event) %>%
summarise(mintime = min(Time),
endtime = max(Time),
x.New = sum(Type == "New"),
x.Repeat = sum(Type == "Repeat"))
# A tibble: 1 x 5
Event mintime endtime x.New x.Repeat
<fctr> <dbl> <dbl> <int> <int>
1 A1 10 21 2 3