2017-10-17 59 views
0

我需要在我的df中应用一个子集(深度1:深度nmN:nmN)函数,该函数应该同时使用两列(t & s)和行作为输入(深度,温度& sal)。我的真实数据有28列至128列170列。我想计算就像一个公式:如何在dplyr乐队中加入LUT?

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 

我在想,这可能是更好的有(深度,温度&公式中使用的行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,]))) 

有没有人有这方面的解决方案?

回答

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)) 
+0

是的!感谢@biomiha!这真的是我想要的,我不知道如何以简单的方式将df转换为长格式,然后我正在考虑使用LUT来解决它。 t取自df,但tdev是一个具有固定值的常量,所以对于我需要做的最后一行:mutate(x = z-(t *(temp - tdev)+ s * sal)))。再次感谢,这为我节省了很多时间来处理我的数据! –