如何将LUT合并到dplyr中?

时间:2017-10-17 12:15:42

标签: r dplyr lookup

我需要将函数应用于我的df中的子集(depth1:depthN over nm1:nmN),该子集应使用列(t& s)和行作为输入(depth,temp& sal)。我的真实数据有170行,28到128行。我想计算一个如下公式:

  x = z- [temp * (temp - tdev) + s * sal]

其中z是观察值

df <- matrix(c( 
1.0277, 1.0051, 1.0059, 1.003,  1.009,  1.00E-04,   -1.20E-05,
1.0019, 0.9841, 0.9769, 0.9809, 0.9815, 9.00E-05,   -1.80E-05,
0.9755, 0.9601, 0.9531, 0.9587, 0.955,  6.00E-05,   -2.00E-05,
0.9522, 0.9364, 0.9296, 0.9322, 0.931,  2.00E-05,   -2.00E-05,
0.2, 0.4, 0.6, 0.8, 1, NA, NA,
15.327, 15.336, 15.356, 15.342, 14.853, NA, NA,
14.908, 14.916, 14.912, 14.9, 17.95, NA, NA
), nrow = 7, ncol = 7, byrow = TRUE, 
dimnames = list(c("nm1","nm2","nm3","nm4","depth","temp","sal"),
            c("depth1","depth2","depth3","depth4","depth5","t","s")))


    df
        depth1  depth2  depth3  depth4  depth5      t          s
 nm1    1.0277  1.0051  1.0059  1.003   1.009   1.00E-04    -1.20E-05
 nm2    1.0019  0.9841  0.9769  0.9809  0.9815  9.00E-05    -1.80E-05
 nm3    0.9755  0.9601  0.9531  0.9587  0.955   6.00E-05    -2.00E-05
 nm4    0.9522  0.9364  0.9296  0.9322  0.931   2.00E-05    -2.00E-05
 depth  0.2     0.4     0.6     0.8     1       NA          NA
 temp   15.327  15.336  15.356  15.342  14.853  NA          NA
 sal    14.908  14.916  14.912  14.95   17.95   NA          NA

我认为将方程中使用的行(depth,temp&amp; sal)放在另一个df(df2)中并将它们从第一个中删除,使用相应的变量depth1:DepthN并用作LUT可能更好如下所示:

 nm <- c("nm1", "nm2","nm3","nm4")
 df1<-df[nm, ]

 df1
        depth1  depth2  depth3  depth4  depth5      t          s
 nm1    1.0277  1.0051  1.0059  1.003   1.009   1.00E-04    -1.20E-05
 nm2    1.0019  0.9841  0.9769  0.9809  0.9815  9.00E-05    -1.80E-05
 nm3    0.9755  0.9601  0.9531  0.9587  0.955   6.00E-05    -2.00E-05
 nm4    0.9522  0.9364  0.9296  0.9322  0.931   2.00E-05    -2.00E-05

 list2 <- c("depth", "temp","sal")
 df2   <- subset(df,rownames(df) %in% list2, select = depth1:depth5)

 df2    depth1   depth2  depth3  depth4  depth5 
 depth   0.2     0.4     0.6     0.8     1      
 temp    15.327  15.336  15.356  15.342  14.853 
  sal    14.908  14.916  14.912  14.95   17.95  

我在dplyr中试过这个,没有成功:

  tdev <- 17.2
  df3<-transmute_at(df, vars(depth1:depth5), funs(.-abs(t*(df2[2,]-   tdev)+s*df2[3,])))

有人有解决方案吗?

1 个答案:

答案 0 :(得分:0)

这需要一些数据整理:

library(tidyverse)
df <- as.data.frame(df) %>% 
  rownames_to_column %>% 
  as_tibble  #convert to tibble (not sure why you'd want a matrix?)

这就是我假设你需要的......不确定t和tdev是否是同一个东西,如果你需要任何分组。

df %>% 
  dplyr::filter(rowname != "depth",
                rowname != "temp",
                rowname != "sal") %>% 
  gather(var, z, -rowname, -t, -s) %>% ## filter from wide to long (i.e. tidy) format
  full_join(df %>% 
              dplyr::select(-t, -s) %>% 
              dplyr::filter(!grepl("nm", rowname)) %>% 
              gather(var, val, -rowname) %>% 
              spread(key = rowname, val)) %>% ## join to the rest of your df
  mutate(x = z- (temp * (temp - t) + s * sal))