我对 R 还很陌生,所以请耐心等待并感谢您的帮助!我想使用一个执行多个计算的函数,这些计算跨数据框中的列进行计算,并创建保存最终计算的新列。我想在数据帧列表中实现此函数,但是,当我尝试使用 lapply 时,我收到一条错误,指出缺少第一个列名称且没有默认值。我知道这一定是我如何格式化函数的问题,但是我真的很难找到解决方案,任何人都可以指出我正确的方向吗?
#create example data frames, my real data frames are named similarly, with an identical names and a unique id (i.e. example_df_uniqueidnumber), each data frame has columns named identically
df1 <- data.frame(pt1_X = c(1,2,3), pt2_X = c(1,2,3), pt1_Y = c(1,2,3), pt2_Y =c(1,2,3))
df2 <- data.frame(pt1_X = c(1,2,3), pt2_X = c(1,2,3), pt1_Y = c(1,2,3), pt2_Y =c(1,2,3))
#create my example function
#NOTE: I call the data "data" (instead of df1 or df2), because I am unsure of what to use instead, as each file name is different due to the unique identifier
calculate_angles1 <- function(data, pt1_X, pt1_Y, pt2_X, pt2_Y) {
data$Mx <- (data[[pt1_X]] - data[[pt2_X]])
data$My <- (data[[pt1_Y]] - data[[pt2_Y]])
return(data)
}
#create my list of data frames
new_list <- list(df1, df2)
#use lapply to attempt to apply my function to my list of data frames
AoA <- lapply(new_list, calculate_angles1)
运行 lapply 函数后,我收到此错误消息..
Error in (function(x, i, exact) if (is.matrix(i)) as.matrix(x)[[i]] else .subset2(x, :
argument "pt1_X" is missing, with no default
请尝试以下代码
new_list <- list(df1, df2)
map(new_list, \(x) {
x <- x %>% mutate(mx=pt1_X-pt2_X, my=pt1_Y-pt2_Y)
return(x)
})
[[1]]
pt1_X pt2_X pt1_Y pt2_Y mx my
1 1 1 1 1 0 0
2 2 2 2 2 0 0
3 3 3 3 3 0 0
[[2]]
pt1_X pt2_X pt1_Y pt2_Y mx my
1 1 1 1 1 0 0
2 2 2 2 2 0 0
3 3 3 3 3 0 0
要使用
calculate_angles2
,您需要引用列名称:
calculate_angles2(df1, 'pt1_X', 'pt2_X', 'pt1_Y', 'pt2_Y')
或者,如果您坚持使用不带引号的名称,您可以在基本 R 中这样做:
calculate_angles3 <- function(data, a, b, c, d){
data$Mx <- eval(substitute(a-b), data)
data$My <- eval(substitute(c-d), data)
data
}
calculate_angles2(df1, 'pt1_X', 'pt2_X', 'pt1_Y', 'pt2_Y')
(或使用 {rlang} 提供的附加功能进行非标准评估)