根据r中其他列的值和条件计算一个新列



我在r中加载了一个这样的csv表

<表类="年代桌子">方法SOCSOC2tbody><WB1.5<Com/td>2.5法则3.1

试试这个:

library(dplyr)
df <- data.frame(method = c("WB", "Com", "LOI"), SOC = c(1.5, 2.5, 3.1))
df %>% mutate(SOC2 = case_when(
method == "WB" ~ SOC * 10, 
method == "Com" ~ SOC * 20, 
method == "LOI" ~ SOC * 30
))
method SOC SOC2
1     WB 1.5   15
2    Com 2.5   50
3    LOI 3.1   93

你可以这样做:

wbIndices <- df$method == "WB"
ComIndices <- df$method == "Com"
LOIIndices <- df$method == "LOI"
df$SOC2 <- df$SOC[wbIndices]*10
df$SOC2 <- df$SOC[ComIndices]*20
df$SOC2 <- df$SOC[LOIIndices]*30

最新更新