在df1中创建一个新的类别变量,该变量将考虑一个df1变量和多个df2变量并具有多种条件

时间:2019-05-17 23:35:30

标签: r dataframe dplyr tidyverse lubridate

我有一个类似于thisthis帖子中使用的数据框。原因是因为我需要在数据集中创建三个不同的变量,并且我对每个问题都发表了不同的文章,因为它们的处理方式互不相同。

df1总结了不同地方不同时间的鱼类的深度。 df2总结了从表面到39米深度的时间间隔(每三小时)的电流强度,间隔为8​​米(m0-7m8-15m16-23,{ {1}}和m24-31)在特定位置。例如:

m32-39

我想在df1<-data.frame(Datetime=c("2016-08-01 15:34:07","2016-08-01 16:25:16","2016-08-01 17:29:16","2016-08-01 18:33:16","2016-08-01 20:54:16","2016-08-01 22:48:16"),Site=c("BD","BD","BD","BD","BD","BD"),Ind=c(16,17,19,16,17,16), Depth=c(5.3,24,36.4,42,NA,22.1)) df1$Datetime<-as.POSIXct(df1$Datetime, format="%Y-%m-%d %H:%M:%S",tz="UTC") > df1 Datetime Site Ind Depth 1 2016-08-01 15:34:07 BD 16 5.3 2 2016-08-01 16:25:16 BD 17 24.0 3 2016-08-01 17:29:16 BD 19 36.4 4 2016-08-01 18:33:16 BD 16 42.0 5 2016-08-01 20:54:16 BD 17 NA 6 2016-08-01 22:48:16 BD 16 22.1 df2<-data.frame(Datetime=c("2016-08-01 12:00:00","2016-08-01 15:00:00","2016-08-01 18:00:00","2016-08-01 21:00:00","2016-08-02 00:00:00"), Site=c("BD","BD","BD","BD","BD"),var1=c(2.75,4,6.75,2.25,4.3),var2=c(3,4,4.75,3,2.1),var3=c(2.75,4,9.8,2.25,1.4),var4=c(3.25,3,6.5,8.9,3.4),var5=c(3,4,2.3,2.6,1.7)) df2$Datetime<-as.POSIXct(df2$Datetime, format="%Y-%m-%d %H:%M:%S",tz="UTC") colnames(df2)<-c("Datetime","Site","m0-7","m8-15","m16-23","m24-31","m32-39") > df2 Datetime Site m0-7 m8-15 m16-23 m24-31 m32-39 1 2016-08-01 12:00:00 BD 2.75 3.00 2.75 3.25 3.0 2 2016-08-01 15:00:00 BD 4.00 4.00 4.00 3.00 4.0 3 2016-08-01 18:00:00 BD 6.75 4.75 9.80 6.50 2.3 4 2016-08-01 21:00:00 BD 2.25 3.00 2.25 8.90 2.6 5 2016-08-02 00:00:00 BD 4.30 2.10 1.40 3.40 1.7 中创建一个名为df1的新变量,该变量反映了鱼是否避免了HIGH CURRENTS。我将Outside_currents列定义为“告诉我,如果鱼有机会存在或不存在,那么它会避免处于高电流层中”。我的鱼总是在大于15米的深度处移动,因此对于此计算,我只考虑了最后三列(Outside_currentsm16-23m24-31)。

将其转化为数学:

m32-39:“当三层中的一层或两层(Outside_currentsm16-23m24-31)的电流强度比另一层大三倍或二,鱼在外面吗?”。

可能的答案是:

  1. m32-39:鱼在当前强度比其他一层或两层低三倍的层中。
  2. Yes:鱼在一层或多层中,其电流强度是其余部分的三倍。
  3. No:不存在该条件时(没有任何层的电流强度是其他层的3倍),或者变量深度为“ NA”。

我希望这样:

NA

2 个答案:

答案 0 :(得分:0)

我想我对您的问题有所遗漏。看来您只是在看df2,并且只要电流标记比一个深度高3倍,电流就会标记为其他深度之一低3倍。我把这个放在一起。看看它是否可以帮助您入门。

library(tidyverse)

outside_calcs <-
  df2 %>% 
  gather(depth, value, m16_23:m32_39) %>% 
  left_join(df2) %>% 
  mutate(
    comp_16 = m16_23/value,
    comp_24 = m24_31/value,
    comp_32 = m32_39/value,
    min_diff = pmin(comp_16, comp_24, comp_32),
    max_diff = pmax(comp_16, comp_24, comp_32)
  ) %>% 
  mutate(
    outside_currents = 
      case_when(
        min_diff < 0.33 ~ "Yes",
        max_diff > 3 ~ "No",
        TRUE ~ NA_character_
      )
  )

#            Datetime Site  depth value m16_23 m24_31 m32_39 comp_16 comp_24 comp_32 min_diff max_diff outside_currents
# 2016-08-18 21:00:00   BD m16_23  2.25   2.25    8.9    2.6   1.000    3.96   1.156    1.000     3.96               No
# 2016-08-18 21:00:00   BD m24_31  8.90   2.25    8.9    2.6   0.253    1.00   0.292    0.253     1.00              Yes
# 2016-08-18 21:00:00   BD m32_39  2.60   2.25    8.9    2.6   0.865    3.42   1.000    0.865     3.42               No
final_outside <-
  outside_calcs %>% 
  mutate(depth = str_replace(depth, "m", "c")) %>% 
  select(
    Datetime, Site,
    depth, outside_currents
  ) %>% 
  spread(depth, outside_currents) %>% 
  left_join(df2) %>% 
  select(Datetime, Site, starts_with("m"), starts_with("c"))

final_outside  

#            Datetime Site m16_23 m24_31 m32_39 c16_23 c24_31 c32_39
# 2016-08-18 12:00:00   BD   2.75   3.25    3.0   <NA>   <NA>   <NA>
# 2016-08-18 15:00:00   BD   4.00   3.00    4.0   <NA>   <NA>   <NA>
# 2016-08-18 18:00:00   BD   9.80   6.50    2.3    Yes   <NA>     No
# 2016-08-18 21:00:00   BD   2.25   8.90    2.6     No    Yes     No
# 2016-08-19 00:00:00   BD   1.40   3.40    1.7   <NA>   <NA>   <NA>

答案 1 :(得分:0)

解决方案:

library(data.table)

library(lubridate)

library(dplyr)

df1<-data.frame(Datetime=c("2016-08-01 12:34:07","2016-08-01 15:34:07","2016-08-01 16:25:16","2016-08-01 17:29:16","2016-08-01 18:33:16","2016-08-01 19:23:16","2016-08-01 20:01:16","2016-08-01 20:54:16","2016-08-01 22:48:16","2016-08-01 23:48:16","2016-08-02 01:07:16"), Site=c("BD","BD","HG","BD","BD","BD","BD","BD","BD","HG","BD"),Ind=c(16,16,17,19,16,16,17,16,16,17,16), Depth=c(15.50,5.30,24.00,36.40,42.00,25.00,NA,22.10,54.00,27.00,21.50))
df1$Datetime<-as.POSIXct(df1$Datetime, format="%Y-%m-%d %H:%M:%S",tz="UTC")
df1$Datetime_rounded<-round_date(df1$Datetime, "3 hour")

df2<-data.frame(Datetime=c("2016-08-01 12:00:00","2016-08-01 15:00:00","2016-08-01 18:00:00","2016-08-01 21:00:00","2016-08-02 00:00:00"), 
            Site=c("BD","BD","BD","BD","BD"),
            var1=c(2.75,4.00,6.75,2.25,4.30),
            var2=c(3.80,7.75,4.75,3.00,2.10),
            var3=c(2.20,4.30,6.80,2.25,3.40),
            var4=c(5.40,1.10,2.25,3.30,6.50),
            var5=c(7.30,5.20,1.30,2.60,1.70))
df2$Datetime<-as.POSIXct(df2$Datetime, format="%Y-%m-%d %H:%M:%S",tz="UTC")
colnames(df2)<-c("Datetime","Site","m0-7","m8-15","m16-23","m24-31","m32-39")

df1<-df1[,c(1,5,2,3,4)] # Rearrange the data frame

setDT(df1) # We convert into data.table
setDT(df2)

setkey(df1, Site, Datetime_rounded) # We indicate the key variables.
setkey(df2, Site, Datetime)

df_merge = df2[df1, roll = -Inf] # Associate one table with the other.
df_merge<-df_merge[,c(8,2,9,10,3:7)] # Rearrange the data.table

df_merge[, Outside_current := case_when(
  Site != "BD" ~ "NA",
  Depth == "NA" ~ "NA",
  Depth < 15 ~ "NA",
  Depth >= 15 & Depth < 24 & (`m16-23`*3 < `m24-31` | `m16-23`*3 < `m32-39` | `m16-23`*3 < (`m24-31`+`m32-39`)/2 ) ~ "Yes",
  Depth >= 24 & Depth < 32 & (`m24-31`*3 < `m16-23` | `m24-31`*3 < `m32-39` | `m24-31`*3 < (`m16-23`+`m32-39`)/2 ) ~ "Yes",
  Depth >= 32 & (`m32-39`*3 < `m16-23` | `m32-39`*3 < `m24-31` | `m32-39`*3 < (`m16-23`+`m24-31`)/2 ) ~ "Yes",
  Depth >= 24 & (`m16-23`*3 < `m24-31` | `m16-23`*3 < `m32-39` | `m16-23`*3 < (`m24-31`+`m32-39`)/2 ) ~ "No",
  (Depth >= 15 & Depth <24 | Depth >= 32) & (`m24-31`*3 < `m16-23` | `m24-31`*3 < `m32-39`  | `m24-31`*3 < (`m16-23`+`m32-39`)/2 ) ~ "No",
  Depth < 32 & (`m32-39`*3 < `m16-23` | `m32-39`*3 < `m24-31` | `m32-39`*3 < (`m16-23`+`m24-31`)/2 ) ~ "No",
  T ~ "NA")]

> df_merge
             i.Datetime Site Ind Depth m0-7 m8-15 m16-23 m24-31 m32-39 Outside_current
 1: 2016-08-01 12:34:07   BD  16  15.5 2.75  3.80   2.20   5.40    7.3             Yes
 2: 2016-08-01 15:34:07   BD  16   5.3 4.00  7.75   4.30   1.10    5.2              NA
 3: 2016-08-01 17:29:16   BD  19  36.4 6.75  4.75   6.80   2.25    1.3             Yes
 4: 2016-08-01 18:33:16   BD  16  42.0 6.75  4.75   6.80   2.25    1.3             Yes
 5: 2016-08-01 19:23:16   BD  16  25.0 6.75  4.75   6.80   2.25    1.3             Yes
 6: 2016-08-01 20:01:16   BD  17    NA 2.25  3.00   2.25   3.30    2.6              NA
 7: 2016-08-01 20:54:16   BD  16  22.1 2.25  3.00   2.25   3.30    2.6              NA
 8: 2016-08-01 22:48:16   BD  16  54.0 4.30  2.10   3.40   6.50    1.7             Yes
 9: 2016-08-02 01:07:16   BD  16  21.5 4.30  2.10   3.40   6.50    1.7              No
10: 2016-08-01 16:25:16   HG  17  24.0   NA    NA     NA     NA     NA              NA
11: 2016-08-01 23:48:16   HG  17  27.0   NA    NA     NA     NA     NA              NA