我有下一个问题。如果我有下一个数据框
data<- data.frame(
Time= c("2018-01-01", "2018-01-02", "2018-01-03", "2018-01-04", "2018-01-05"),
TEN= c(10,20,11,12,16)
)
,我想使用将TEN <15的连续天数作为新列。
我尝试
waves_min <- function(df, prop, min_value, min_days) {
sum(with(rle(df$Temp > min_temp), values & lengths >= min_days))
}
但它返回该计数的总数,而不是每一行的值。
有什么想法吗?
谢谢
答案 0 :(得分:2)
@Entity
@Table(name = "some_entity")
public final class SomeEntity {
@Column(nullable = false, unique = true, length = 20)
private String externalId;
@Column(nullable = false, length = 50)
private String name;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "parentId", nullable = true)
private SomeEntity parent;
@OneToMany(mappedBy = "parent")
private List<SomeEntity> children; //or Set<>
//returns direct children
public List<SomeEntity> getChildren(){
return children;
}
//returns all children to any level
public List<SomeEntity> getAllChildren(){
getAllChildren(this);
}
//recursive function to walk the tree
private List<SomeEntity> getAllChildren(SomeEntity parent){
List<SomeEntity> allChidren = new ArrayList<>();
for(SomeEntity child : children){
allChildren.add(child);
allChildren.addAll(getAllChildren(child);
}
return allChildren;
}
}
+ dplyr
:data.table::rleid
输出:
library(dplyr)
library(data.table)
data %>%
group_by(ID = data.table::rleid(TEN < 15)) %>%
mutate(Consec_Days = if_else(TEN < 15, row_number(), 0L))
# A tibble: 7 x 4
# Groups: ID [5]
Time TEN ID Consec_Days
<fct> <dbl> <int> <int>
1 2018-01-01 10 1 1
2 2018-01-02 20 2 0
3 2018-01-03 11 3 1
4 2018-01-04 12 3 2
5 2018-01-05 16 4 0
6 2018-01-06 17 4 0
7 2018-01-07 14 5 1
:data.table
输出:
setDT(data)
data[, Consec_Days := ifelse(TEN < 15, 1:.N, 0L), by = rleid(TEN < 15)]
Time TEN Consec_Days
1: 2018-01-01 10 1
2: 2018-01-02 20 0
3: 2018-01-03 11 1
4: 2018-01-04 12 2
5: 2018-01-05 16 0
6: 2018-01-06 17 0
7: 2018-01-07 14 1
:data.table::rleid
输出:
data$Consec_Days <- with(data, ave(TEN, data.table::rleid(TEN < 15),
FUN = function(x) ifelse(x < 15, seq_along(x), 0L)))
Time TEN Consec_Days
1 2018-01-01 10 1
2 2018-01-02 20 0
3 2018-01-03 11 1
4 2018-01-04 12 2
5 2018-01-05 16 0
6 2018-01-06 17 0
7 2018-01-07 14 1
我在OP的示例数据中添加了更多行,以说明这些解决方案适用于所有情况。