如何将 LUT 合并到 dplyr funs 中?
How to incorporate a LUT in a dplyr funs?
我需要将函数应用到我的 df 中的一个子集 (depth1:depthN over nm1:nmN),它应该使用列 (t & s) 和行作为输入 (depth、temp & sal)。我的真实数据有 170 列 x 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
我在想,将等式(深度、温度和盐)中使用的行放在另一个 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,])))
有人对此有解决方案吗?
这需要一些数据整理:
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))
我需要将函数应用到我的 df 中的一个子集 (depth1:depthN over nm1:nmN),它应该使用列 (t & s) 和行作为输入 (depth、temp & sal)。我的真实数据有 170 列 x 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
我在想,将等式(深度、温度和盐)中使用的行放在另一个 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,])))
有人对此有解决方案吗?
这需要一些数据整理:
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))