我需要编写一个函数,它使用正文中的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)对如何写好它有什么帮助吗?谢谢
发布于 2020-03-05 21:57:21
我们可以对值进行paste,转换为symbol,然后进行计算(!!)。如果我们创建了一个字符串,则tidyverse首选的方法是将其转换为symbol,然后进行计算。
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(
>=0 & nm1 >=0 ~ "pos",
>=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)
)发布于 2020-03-05 22:08:36
如果您不熟悉symbol和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 poshttps://stackoverflow.com/questions/60554433
复制相似问题