英文:
How to loop following group_by
问题
Here's the translated R code to generate the desired output:
# 我有以下数据框,其中有3列。
# 数据框代码:
df <- data.frame(
"inst" = rep(c('a','b','c','d'),4),
"swi" = c(rep(0.05,4), rep(0.10,4), rep(0.15,4), rep(0.20,4)),
"val" = c(10,12,8,15,11,14,10,15,11,15,13,15,11,15,16,15)
)
# 数据框输出:
# # A tibble: 16 x 3
# # Groups: inst [4]
# inst swi val
# <fct> <dbl> <dbl>
# 1 a 0.05 10
# 2 b 0.05 12
# 3 c 0.05 8
# 4 d 0.05 15
# 5 a 0.1 11
# 6 b 0.1 14
# 7 c 0.1 10
# 8 d 0.1 15
# 9 a 0.15 11
# 10 b 0.15 15
# 11 c 0.15 13
# 12 d 0.15 15
# 13 a 0.2 11
# 14 b 0.2 15
# 15 c 0.2 16
# 16 d 0.2 15
# 我想为每个唯一的 'inst' 找到 'swi' 值,其中当前 'swi' 值的 'val' 值等于下一个 'swi' 值。
# 如果对于一个 'inst',不满足此条件,则输出应为 '-1'。
# 换句话说,我想计算/生成以下内容:
inst swi
a 0.1
b 0.15
c -1
d 0.05
# 如何生成这个输出?
# 你可以使用以下代码生成所需的输出:
library(dplyr)
result <- df %>%
arrange(inst, swi) %>%
group_by(inst) %>%
mutate(next_val = lead(val)) %>%
filter(val == next_val | is.na(next_val)) %>%
summarise(swi = ifelse(all(is.na(next_val)), -1, unique(swi)))
result
这段代码会生成所需的输出,其中 'inst' 列包含唯一的 'inst' 值,而 'swi' 列包含对应的 'swi' 值。
英文:
I have the following data frame, which has 3 columns.
Data-frame code:
df <- data.frame(
"inst" = rep(c('a','b','c','d'),4),
"swi" = c(rep(0.05,4), rep(0.10,4), rep(0.15,4), rep(0.20,4)),
"val" = c(10,12,8,15,11,14,10,15,11,15,13,15,11,15,16,15)
)
Data-frame output:
# A tibble: 16 x 3
# Groups: inst [4]
inst swi val
<fct> <dbl> <dbl>
1 a 0.05 10
2 b 0.05 12
3 c 0.05 8
4 d 0.05 15
5 a 0.1 11
6 b 0.1 14
7 c 0.1 10
8 d 0.1 15
9 a 0.15 11
10 b 0.15 15
11 c 0.15 13
12 d 0.15 15
13 a 0.2 11
14 b 0.2 15
15 c 0.2 16
16 d 0.2 15
I want to find for each unique 'inst' the 'swi' value, where 'val' value for the current 'swi' value is equal to next one. If this condition is not satisfied for an 'inst', then the output should be '-1'.
In other words, I want to calculate/generate the following:
inst swi
a 0.1
b 0.15
c -1
d 0.05
How can I generate this output?
答案1
得分: 1
使用dplyr
的另一种方法。使用reframe
函数,针对每个inst
,选择第一个val
的差异为零的swi
。如果对于给定的inst
不存在这种情况,将显示NA
- 您可以将其替换为-1。
library(tidyverse)
df %>%
reframe(
swi = swi[diff(val) == 0][1],
.by = inst
) %>%
replace_na(list(swi = -1))
输出
inst swi
1 a 0.10
2 b 0.15
3 c -1.00
4 d 0.05
英文:
Another approach using dplyr
. Using reframe
take the first swi
where the diff
erence of val
is zero, for each inst
. When this is not present for a given inst
, there will be NA
- you can replace this with -1.
library(tidyverse)
df %>%
reframe(
swi = swi[diff(val) == 0][1],
.by = inst
) %>%
replace_na(list(swi = -1))
Output
inst swi
1 a 0.10
2 b 0.15
3 c -1.00
4 d 0.05
答案2
得分: 0
以下是翻译好的部分:
以下代码完成了解决方案的主要部分(即找到与下一个'swi'值的'val'值相等的最小/第一个'swi'值):
df %>%
group_by(inst) %>%
mutate(next_val = lead(val)) %>%
filter(next_val == val) %>%
summarize(ret_swi = min(swi))
这个代码产生以下输出:
# A tibble: 3 × 2
inst ret_swi
<chr> <dbl>
1 a 0.1
2 b 0.15
3 d 0.05
如果有一些'inst'不满足上述条件,还需要将'ret_swi'的值设置为'-1'。
英文:
The following code fulfills the main part of the solution (which is to find the minimum/first 'swi' value, for which the corresponding 'val' value is equal to the one of the next 'swi' value):
df %>%
group_by(inst) %>%
mutate(next_val = lead(val)) %>%
filter(next_val == val) %>%
summarize(ret_swi = min(swi))
This one produces the following output:
# A tibble: 3 × 2
inst ret_swi
<chr> <dbl>
1 a 0.1
2 b 0.15
3 d 0.05
What is missing is setting 'ret_swi' value to '-1' if there are 'inst's which do not fulfill the condition above.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论