Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过R中的另一列条件检索值_R - Fatal编程技术网

通过R中的另一列条件检索值

通过R中的另一列条件检索值,r,R,我需要一些帮助: 我得到了这个df: df <- data.frame(month = c(1,1,1,1,1,2,2,2,2,2), day = c(1,2,3,4,5,1,2,3,4,5), flow = c(2,5,7,8,5,4,6,7,9,2)) month day flow 1 1 1 2 2 1 2 5 3 1 3 7 4 1 4

我需要一些帮助:

我得到了这个df:

df <- data.frame(month = c(1,1,1,1,1,2,2,2,2,2),
             day   = c(1,2,3,4,5,1,2,3,4,5),
             flow  = c(2,5,7,8,5,4,6,7,9,2))

   month day flow
1      1   1    2
2      1   2    5
3      1   3    7
4      1   4    8
5      1   5    5
6      2   1    4
7      2   2    6
8      2   3    7
9      2   4    9
10     2   5    2
这种重复不是问题,我将使用轴函数


tks人

我们可以使用
which.min
返回每个组的“min'imum'flow”索引,并使用该索引获得相应的“day”来创建带有
mutate的列

library(dplyr)
df <- df %>%
       group_by(month) %>% 
       mutate(dayminflowofthemonth = day[which.min(flow)]) %>%
       ungroup
库(dplyr)
df%
分组单位(月)%>%
突变(DayminFlowOfMonth=天[which.min(flow)])%>%
解组
-输出

df
# A tibble: 10 x 4
#   month   day  flow dayminflowofthemonth
#   <dbl> <dbl> <dbl>                <dbl>
# 1     1     1     2                    1
# 2     1     2     5                    1
# 3     1     3     7                    1
# 4     1     4     8                    1
# 5     1     5     5                    1
# 6     2     1     4                    5
# 7     2     2     6                    5
# 8     2     3     7                    5
# 9     2     4     9                    5
#10     2     5     2                    5
df
#一个tibble:10x4
#月日流量日最小月流量
#                     
# 1     1     1     2                    1
# 2     1     2     5                    1
# 3     1     3     7                    1
# 4     1     4     8                    1
# 5     1     5     5                    1
# 6     2     1     4                    5
# 7     2     2     6                    5
# 8     2     3     7                    5
# 9     2     4     9                    5
#10     2     5     2                    5

另一个在
dplyr管道内使用索引的选项:

library(dplyr)
#Code
newdf <- df %>% group_by(month) %>% mutate(Val=day[flow==min(flow)][1])
库(dplyr)
#代码
newdf%分组依据(月)%>%变异(Val=day[flow==min(flow)][1])
输出:

# A tibble: 10 x 4
# Groups:   month [2]
   month   day  flow   Val
   <dbl> <dbl> <dbl> <dbl>
 1     1     1     2     1
 2     1     2     5     1
 3     1     3     7     1
 4     1     4     8     1
 5     1     5     5     1
 6     2     1     4     5
 7     2     2     6     5
 8     2     3     7     5
 9     2     4     9     5
10     2     5     2     5
#一个tible:10 x 4
#分组:月[2]
月日流量值
1     1     1     2     1
2     1     2     5     1
3     1     3     7     1
4     1     4     8     1
5     1     5     5     1
6     2     1     4     5
7     2     2     6     5
8     2     3     7     5
9     2     4     9     5
10     2     5     2     5

这是一个使用ave的基本R选项

transform(
  df,
  dayminflowofthemonth = ave(day*(ave(flow,month,FUN = min)==flow),month,FUN = max)
)

   month day flow dayminflowofthemonth
1      1   1    2                    1
2      1   2    5                    1
3      1   3    7                    1
4      1   4    8                    1
5      1   5    5                    1
6      2   1    4                    5
7      2   2    6                    5
8      2   3    7                    5
9      2   4    9                    5
10     2   5    2                    5

还有一个基本R方法:

df$dayminflowofthemonth <- by(
  df,
  df$month,
  function(x) x$day[which.min(x$flow)]
)[df$month]

df$dayminflowofmonthperfect。但是我能问点什么吗?我如何得到最小值后的两天?如果“4”是DayminFlowMonth,我想检索“1”,例如(我将在30/31天内使用它数月,因此它需要在帐户中)@BryanSouza。每天都是连续的吗?。对于第一个“月”,您需要
4
,如果没有精确匹配,即您有3和5,那么应该选择哪一个return@BryanSouza另外,对于第二个月,“天”的值是多少,因为5是最大的“天”,因为这些天是一年中的几天,所以每个月都有变化(30,31,28天…)。我有一个按日期顺序排列的列,所以我需要在该列中“跳过”7天,然后获取value@BryanSouza请你更新一个新的例子,以便我可以测试它。谢谢我在想'df%>%groupby(month)%>%mutate(dayminflowplus2=min(max(day),day+3))[which.min(flow)]这很好,但是如果第一个值(day1)是NA,则返回NA。(在min()中na.rm=True不起作用)
df$dayminflowofthemonth <- by(
  df,
  df$month,
  function(x) x$day[which.min(x$flow)]
)[df$month]