我需要编写一个函数,该函数使用主体中的string参数创建列的名称。 例如,
dt1 <- data.table( Col_1999_L = c(6,7,8,9,10),
Col_1999_R = c(1,-2,3,-4,5),
Col_2001_L = c(-8-3,5, 2,-1),
Col_2001_R = c(-1,2,-3,4,5)
)
在这里,1999、2001确实代表了几年。 我的输出应该像在dt2中一样,我在其中创建了检查某些条件的新列。
dt2 <- dt1 %>%
select(c("Col_1999_L", "Col_1999_R")) %>%
mutate(
New1 = if_else(Col_1999_L >= 0, "pos", "neg"),
New2 = case_when(
Col_1999_L >=0 & Col_1999_R >=0 ~ "pos",
Col_1999_L >=0 & Col_1999_R < 0 ~ "neg",
))
dt2
实际上我需要编写一个函数,该函数每年创建一个类似于df2的表。
我尝试了类似的操作(这是错误的):
my_function <- function(dt, Year) {
dt %>%
mutate(
New1 = if_else(Col_"Jahr"_L >= 0, "pos", "neg"),
New2 = case_when(
Col_"Year"_L >=0 & Col_"Year"_R >=0 ~ "pos",
Col_"Year"_L >=0 & Col_"Year"_R < 0 ~ "neg"))
}
my_function(dt1, 1999)
如何正确编写任何帮助? 谢谢
答案 0 :(得分:2)
我们可以paste
的值,转换为sym
bol,然后求值(!!
)。如果我们创建了字符串,则tidyverse首选方法是将其转换为sym
bol,然后求值。
library(dplyr)
my_function <- function(dt, Year) {
nm1 <- rlang::sym(paste0("Col_", Year, "_R"))
nm2 <- rlang::sym(paste0("Col_", Year, "_L"))
dt %>%
mutate(
New1 = if_else(!! nm2 >= 0, "pos", "neg"),
New2 = case_when(
!!nm2 >=0 & !!nm1 >=0 ~ "pos",
!!nm2 >=0 & !!nm1 < 0 ~ "neg"))
}
my_function(dt1, 1999)
# Col_1999_L Col_1999_R Col_2001_L Col_2001_R New1 New2
#1: 6 1 -8 -1 pos pos
#2: 7 -2 -3 2 pos neg
#3: 8 3 5 -3 pos pos
#4: 9 -4 2 4 pos neg
#5: 10 5 -1 5 pos pos
或者,如果我们传递完整的未加引号的列名,则可以使用curly-curly
运算符({{}}
)
my_function <- function(dt, colnm1, colnm2) {
dt %>%
mutate(New1 = if_else({{colnm2}} > 0, "pos", "neg"),
New2 = case_when({{colnm2}} >= 0 & {{colnm1}} >=0 ~ "pos",
{{colnm2}} >= 0 & {{colnm1}} < 0 ~ "neg"))
}
my_function(dt1, Col_1999_R, Col_1999_L)
# Col_1999_L Col_1999_R Col_2001_L Col_2001_R New1 New2
#1: 6 1 -8 -1 pos pos
#2: 7 -2 -3 2 pos neg
#3: 8 3 5 -3 pos pos
#4: 9 -4 2 4 pos neg
#5: 10 5 -1 5 pos pos
dt1 <- data.table( Col_1999_L = c(6,7,8,9,10),
Col_1999_R = c(1,-2,3,-4,5),
Col_2001_L = c(-8, -3,5, 2,-1),
Col_2001_R = c(-1,2,-3,4,5)
)
答案 1 :(得分:1)
如果您不熟悉sym
bol和bang bang
运算符,则可以使用get
:
my_function <- function(dt,year){
R <- sprintf("Col_%d_R",year) # same as paste0("Col_",year,"_R") but simplified
L <- sprintf("Col_%d_L",year)
dt %>%
mutate(
New1 = if_else(get(R,dt) >= 0, "pos", "neg"), # although get(R) works, use get(R,dt)
New2 = case_when(
get(L,dt) >=0 & get(R,dt) >=0 ~ "pos",
get(L,dt)>=0 & get(R,dt) < 0 ~ "neg",
))
}
my_function(dt1,1999)
Col_1999_L Col_1999_R Col_2001_L Col_2001_R New1 New2
1 6 1 -8 -1 pos pos
2 7 -2 -3 2 neg neg
3 8 3 5 -3 pos pos
4 9 -4 2 4 neg neg
5 10 5 -1 5 pos pos