我有一个数据框,其中有“线性”变量的值,并且有30个不同的组。我想为数据框中的每个组计算/计算最大递增/递减序列的元素。
[INFO] Verbose not supported since maven-dependency-plugin 3.0
[INFO] com.frankmoley.lil:new-features-app:jar:1.0.0-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:2.0.5.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.0.5.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.5.RELEASE:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
[INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
[INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] | | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.19:runtime
[INFO] | +- org.springframework.boot:spring-boot-actuator-autoconfigure:jar:2.0.5.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-actuator:jar:2.0.5.RELEASE:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.6:compile
[INFO] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] | | | \- com.fasterxml.jackson.core:jackson-core:jar:2.9.6:compile
[INFO] | | +- org.springframework:spring-context:jar:5.0.9.RELEASE:compile
[INFO] | | \- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.6:compile
[INFO] | \- io.micrometer:micrometer-core:jar:1.0.6:compile
[INFO] | +- org.hdrhistogram:HdrHistogram:jar:2.1.10:compile
[INFO] | \- org.latencyutils:LatencyUtils:jar:2.0.3:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.0.5.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.0.5.RELEASE:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.6:compile
[INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.6:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.5.RELEASE:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.1:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.1:compile
[INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.1:compile
[INFO] | +- org.hibernate.validator:hibernate-validator:jar:6.0.12.Final:compile
[INFO] | | +- javax.validation:validation-api:jar:2.0.1.Final:compile
[INFO] | | +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
[INFO] | | \- com.fasterxml:classmate:jar:1.3.4:compile
[INFO] | +- org.springframework:spring-web:jar:5.0.9.RELEASE:compile
[INFO] | | \- org.springframework:spring-beans:jar:5.0.9.RELEASE:compile
[INFO] | \- org.springframework:spring-webmvc:jar:5.0.9.RELEASE:compile
[INFO] | +- org.springframework:spring-aop:jar:5.0.9.RELEASE:compile
[INFO] | \- org.springframework:spring-expression:jar:5.0.9.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-thymeleaf:jar:2.0.5.RELEASE:compile
[INFO] | +- org.thymeleaf:thymeleaf-spring5:jar:3.0.9.RELEASE:compile
[INFO] | | +- org.thymeleaf:thymeleaf:jar:3.0.9.RELEASE:compile
[INFO] | | | +- org.attoparser:attoparser:jar:2.0.4.RELEASE:compile
[INFO] | | | \- org.unbescape:unbescape:jar:1.1.5.RELEASE:compile
[INFO] | | \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] | \- org.thymeleaf.extras:thymeleaf-extras-java8time:jar:3.0.1.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-devtools:jar:2.0.5.RELEASE:runtime
[INFO] | +- org.springframework.boot:spring-boot:jar:2.0.5.RELEASE:compile
[INFO] | \- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.5.RELEASE:compile
[INFO] +- org.projectlombok:lombok:jar:1.16.22:compile (optional)
[INFO] \- org.springframework.boot:spring-boot-starter-test:jar:2.0.5.RELEASE:test
[INFO] +- org.springframework.boot:spring-boot-test:jar:2.0.5.RELEASE:test
[INFO] +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.0.5.RELEASE:test
[INFO] +- com.jayway.jsonpath:json-path:jar:2.4.0:test
[INFO] | \- net.minidev:json-smart:jar:2.3:test
[INFO] | \- net.minidev:accessors-smart:jar:1.2:test
[INFO] | \- org.ow2.asm:asm:jar:5.0.4:test
[INFO] +- junit:junit:jar:4.12:test
[INFO] +- org.assertj:assertj-core:jar:3.9.1:test
[INFO] +- org.mockito:mockito-core:jar:2.15.0:test
[INFO] | +- net.bytebuddy:byte-buddy:jar:1.7.11:test
[INFO] | +- net.bytebuddy:byte-buddy-agent:jar:1.7.11:test
[INFO] | \- org.objenesis:objenesis:jar:2.6:test
[INFO] +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] +- org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO] | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] +- org.springframework:spring-core:jar:5.0.9.RELEASE:compile
[INFO] | \- org.springframework:spring-jcl:jar:5.0.9.RELEASE:compile
[INFO] +- org.springframework:spring-test:jar:5.0.9.RELEASE:test
[INFO] \- org.xmlunit:xmlunit-core:jar:2.5.1:test
组(列:系列) 结果/输出应该是我想要保存两个矢量的地方:
Linear | Series | Basal
91 | Night 1 | decrease
78 | Night 1 | decrease
82 | Night 1 | increase
75 | Night 1 | decrease
71 | Night 1 | decrease
73 | Night 1 | increase
76 | Night 1 | increase
103 | Night 1 | increase
112 | Night 1 | increase
.
.
.
答案 0 :(得分:0)
我不太确定您期望的输出格式,但是也许您可以执行以下操作:
library(tidyverse)
df.res <- df %>%
group_by(Series) %>%
mutate(grp = cumsum(c(0, diff(Basal)) != 0)) %>%
add_count(Basal, grp) %>%
group_by(Basal) %>%
filter(n == max(n))
df.res
## A tibble: 8 x 5
## Groups: Basal [2]
# Linear Series Basal grp n
# <int> <fct> <fct> <int> <int>
#1 91 Night 1 decrease 0 2
#2 78 Night 1 decrease 0 2
#3 75 Night 1 decrease 2 2
#4 71 Night 1 decrease 2 2
#5 73 Night 1 increase 3 4
#6 76 Night 1 increase 3 4
#7 103 Night 1 increase 3 4
#8 112 Night 1 increase 3 4
这将返回data.frame
/ tibble
,其中grp
列标记不同的增减组,而n
返回该组中增减的数量。然后,很容易为最大增加/减少组下面的条目进行过滤。如果是平局,它将报告所有具有相同最大增加/减少量的组。
根据您想对下游数据进行的操作,例如将df.res
除以grp
df.res %>%
split(., .$grp)
#$`0`
## A tibble: 2 x 5
## Groups: Basal [1]
# Linear Series Basal grp n
# <int> <fct> <fct> <int> <int>
#1 91 Night 1 decrease 0 2
#2 78 Night 1 decrease 0 2
#
#$`2`
## A tibble: 2 x 5
## Groups: Basal [1]
# Linear Series Basal grp n
# <int> <fct> <fct> <int> <int>
#1 75 Night 1 decrease 2 2
#2 71 Night 1 decrease 2 2
#
#$`3`
## A tibble: 4 x 5
## Groups: Basal [1]
# Linear Series Basal grp n
# <int> <fct> <fct> <int> <int>
#1 73 Night 1 increase 3 4
#2 76 Night 1 increase 3 4
#3 103 Night 1 increase 3 4
#4 112 Night 1 increase 3 4