根据多个条件选择行

问题描述 投票:1回答:4

我有一个df

set.seed(123)
df <- data.frame(loc.id = rep(1:9, each = 9), month = rep(1:9,times = 9), 
                 x = runif(81, min = 0, max = 5))

这是一个有9个位置的数据框。对于每个位置,我有9个月,每个月,有一个值x。

对于每个位置,我想根据以下标准选择一个月:

1)检查哪些月份(不包括第9个月)x> 1,然后选择最接近第9个月的月份。例如,如果位置1,则x的值为

  4.56, 3.41, 0.82, 2.31, 3.75, 4.75, 1.22, 2.98, 1.17

然后第1,2,4,5,6,7,8个月的x> 1,从这几个月开始,第8个月最接近第9个月。因此将选择第8个月

2)如果没有月份x> 1,只需选择具有最高x值的月份。例如:

如果是某个位置,x是

  0.8, 0.6, 0.95, 0.4, 0.88, 0.7, 0.6, 0.45, 0.3

然后将选择第3个月(x = 0.95)

我试过这个:

  library(dplyr)
  df %>% filter(month != 9) %>% # removes the 9 month so that only the 8 months are evaluated 
        group_by(loc.id) %>% 
        mutate(select.month = x > 1) %>% # mark those months where x > 1
        filter(select.month == TRUE) %>% # select those months where x > 1 is true
        mutate(dif = 9 - month) %>%# subtract each month from 9 to check which one is closest to 9
        summarise(month.id = min(dif)) # select the months which is closest to month 9

但是,在上面的函数中,我无法检查那些月份的值是否小于1的位置。我的问题是如何更改上面的代码,以便在x> 1时都检查条件2

r if-statement dplyr data.table
4个回答
1
投票
set.seed(123)
> df <- data.frame(loc.id = rep(1:9, each = 9), month = rep(1:9,times = 9), 
                  x = runif(81, min = 0, max = 5))
> set.seed(123)
> df=rbind(df,cbind(loc.id=10,month=1:9 , x=runif(9)))




> df%>%group_by(loc.id)%>%mutate(x=replace(x,9,0),y=cumsum(x>1))%>%
+     summarise(y=ifelse(all(!y),which.max(x),which.max(y)))
# A tibble: 10 x 2
   loc.id     y
    <dbl> <int>
 1      1     8
 2      2     8
 3      3     8
 4      4     7
 5      5     8
 6      6     8
 7      7     7
 8      8     8
 9      9     7
10     10     5

1
投票

我稍微修改了你的数据框,因为没有任何一个loc.ids只有少于1的月份。

df %>% 
  group_by(loc.id) %>%
  filter(month != 9) %>% 
  mutate(all_x_less_1 = all(x < 1)) %>% 
  filter(all_x_less_1 | x > 1) %>% 
  filter(month == if_else(all_x_less_1, month[which.max(x)], month[which.min(9 - month)]))

# A tibble: 9 x 4
# Groups:   loc.id [9]
#   loc.id month     x all_x_less_1
#    <int> <int> <dbl> <lgl>       
# 1      1     8 4.46  F           
# 2      2     7 2.25  F           
# 3      3     8 1.18  F           
# 4      4     5 1.13  F           
# 5      5     1 0.758 T           
# 6      6     5 0.715 T           
# 7      7     5 0.639 T           
# 8      8     2 0.509 T           
# 9      9     1 0.395 T         

诀窍不仅是过滤x < 1,还要过滤x中的所有loc.id是否小于1.然后在if_else中使用filter调用,您可以根据所有x是否小于1来指定您的标准。

数据

set.seed(123)
df <- data.frame(loc.id = rep(1:9, each = 9), month = rep(1:9,times = 9), 
                 x = runif(81, min = 0, max = 5))

df <- df %>% 
  mutate(x = x/loc.id)

1
投票
library(data.table)
setDT(d)
d[ , {
  ix <- x > 1 & month != 9 
  .(month = if(any(ix)) last(month[ix]) else month[which.max(x)])
}, by = loc.id]

说明:

对于每个组(by = loc.id),获取索引,其中x> 1,不包括第9个月(x > 1 & month != 9)。如果任何此类指数为真(if(any(ix))),请选择其中的最后一个月(last(month[ix]))。否则选择对应于max x(else month[which.max(x)])的月份。


0
投票

一个可能的解决方案是ifelse,如果位置8> 1.0,则位置8,否则在排除第9行之后该行的最大值。

一个月的例子:

month1 <- filter(df, loc.id == 1) month1 <- month1[1:8, ] df1 <- ifelse(month1[8,3] > 1.0, month1[8,3], max(month1$x))

© www.soinside.com 2019 - 2024. All rights reserved.