R将公式和条件逻辑应用于数据帧

R将公式和条件逻辑应用于数据帧,r,dplyr,tidyverse,mutate,R,Dplyr,Tidyverse,Mutate,我有一个数据框,其中包含样本的湿重和干重的数字变量,比如土壤。在此数据帧中,一些值等于0,另一些值大于零。我想对变量应用一个公式来创建一个新的变量,但只针对大于零的数据对。到目前为止,我已经尝试了dplyr的filter功能 我想使用以下公式创建新变量: 含水量=(湿重-干重)/湿重 以下是我迄今为止尝试过的代码: dry_weight <- c(0,1,0,2,0,3,4,5,6,7) wet_weight <- c(1,0,2,4,0,1,4,0,5,0) weights <

我有一个数据框,其中包含样本的湿重和干重的数字变量,比如土壤。在此数据帧中,一些值等于0,另一些值大于零。我想对变量应用一个公式来创建一个新的变量,但只针对大于零的数据对。到目前为止,我已经尝试了
dplyr
filter
功能

我想使用以下公式创建新变量:

含水量=(湿重-干重)/湿重

以下是我迄今为止尝试过的代码:

dry_weight <- c(0,1,0,2,0,3,4,5,6,7)
wet_weight <- c(1,0,2,4,0,1,4,0,5,0)
weights <- data.frame(dry_weight, wet_weight)
weights$moisture <- weights %>%
  filter(weights$wet_weight > 0, weights$dry_weight >0) %>%
  mutate((weights$wet_weight-weights$dry_weight)/weights$wet_weight)

如有任何建议,将不胜感激

我希望这会让你开始

首先,每次使用管道(
%%>%%
)时,无需一直键入
weights$

其次,使用
mutate
,您需要在左手边指定
=

weights %>%
  dplyr::filter(wet_weight > 0 & dry_weight > 0) %>%
  mutate(moisture = (wet_weight - dry_weight)/wet_weight)
#  dry_weight wet_weight moisture
#1          2          4      0.5
#2          3          1     -2.0
#3          4          4      0.0
#4          6          5     -0.2

请记住,如果您想将其分配回
权重
,只需添加
权重,另一种方法就是简单地使用
基本R

weights$moisture <- 
              ifelse(weights$dry_weight*weights$wet_weight > 0
                     , 1-weights$dry_weight/weights$wet_weight
                     , NA)
weights
   dry_weight wet_weight moisture
1           0          1       NA
2           1          0       NA
3           0          2       NA
4           2          4      0.5
5           0          0       NA
6           3          1     -2.0
7           4          4      0.0
8           5          0       NA
9           6          5     -0.2
10          7          0       NA
权重$0
,1-重量$干重/重量$湿重
,NA)
砝码
干重湿重水分
101NA
2110NA
302NA
4           2          4      0.5
500纳
6           3          1     -2.0
7           4          4      0.0
850NA
9           6          5     -0.2
1070NA
ifelse
是一个向量化的
if
ifelse(条件,如果为真,那么这个,如果为假,那么那个)
。在这里,我检查这两个值是否严格大于零,在这种情况下,我返回湿度,否则返回
NA

矢量化方式:

#Initialize column to NA
weights$moisture <- NA
#Get the index where dry_weight > 0 and wet_weight > 0
inds <- with(weights, dry_weight > 0 & wet_weight >0)
#Calculate using the formula and replace the value.
weights$moisture[inds] <- with(weights, 
                          (wet_weight[inds] - dry_weight[inds])/wet_weight[inds])


weights
#   dry_weight wet_weight moisture
#1           0          1       NA
#2           1          0       NA
#3           0          2       NA
#4           2          4      0.5
#5           0          0       NA
#6           3          1     -2.0
#7           4          4      0.0
#8           5          0       NA
#9           6          5     -0.2
#10          7          0       NA
#将列初始化为NA
重量$湿度0,湿重>0
inds 0(湿重>0)
#使用公式计算并替换该值。
重量$湿度[inds]
#Initialize column to NA
weights$moisture <- NA
#Get the index where dry_weight > 0 and wet_weight > 0
inds <- with(weights, dry_weight > 0 & wet_weight >0)
#Calculate using the formula and replace the value.
weights$moisture[inds] <- with(weights, 
                          (wet_weight[inds] - dry_weight[inds])/wet_weight[inds])


weights
#   dry_weight wet_weight moisture
#1           0          1       NA
#2           1          0       NA
#3           0          2       NA
#4           2          4      0.5
#5           0          0       NA
#6           3          1     -2.0
#7           4          4      0.0
#8           5          0       NA
#9           6          5     -0.2
#10          7          0       NA