简体   繁体   English

通过lapply循环将自定义函数应用于R中的数据帧列表

[英]Apply custom functions to list of dataframes in R with lapply loop

I tried to loop a list of df elements through 3 custom functions. 我试图通过3个自定义函数循环显示df元素列表。 It works when I run each df manually but applying lapply fails. 当我手动运行每个df但应用lapply失败时,它可以工作。 FYI: The outcome should result in "expanding" each dataset, which I will later use to crop an image file. 仅供参考:结果应导致“扩展”每个数据集,稍后将用于裁剪图像文件。 The data txt files have an .swc extension but are simply txt files. 数据txt文件的扩展名为.swc,但仅仅是txt文件。 When I run the datasets through the functions manually it works. 当我通过函数手动运行数据集时,它将起作用。 Then I imported multiple files into a list, which also worked. 然后,我将多个文件导入到列表中,该列表也起作用。 However, when I try to use lapply to run each list element through the functions I get the following error 但是,当我尝试使用lapply通过函数运行每个列表元素时,出现以下错误

Here is the actual attempt to apply lapply 这是申请lapply的实际尝试

result<-lapply(df_swc_list, fxn(x))

Practice dfs to use and import into a list 练习DFS以使用并导入列表

df_swc1 <- data.frame(V1 = c(1,2,3,4), V2 = c(17.2,17.2,18.2,18.2), 
V3=c(105.32,106.01,108.89,109.99), V4=c(3,4,4,6), V5=c(1,2,2,2), 
V6=c(7,7,7,7), V7=c(-1,0,1,2))
df_swc2 <- data.frame(V1 = c(1,2,3,4), V2 = c(17.2,17.2,18.2,18.2), 
V3=c(105.32,106.01,108.89,109.99), V4=c(5,5,4,5), V5=c(1,2,2,2), 
V6=c(7,7,7,7), V7=c(-1,0,1,2))
df_swc_list <- list(df_swc1,df_swc2)
df_swc_list_names<-c("control", "variable")
names(df_swc_list)<-gsub("\\.swc$", "",df_swc_list_names)

pythag.opp.leg<-function(Radius){
Diam<-Radius*2
opposite<-sqrt((Diam^2)/2)
opposite.rounded<-round(opposite)
box<-opposite.rounded/2
return(box)
}

swc.fxn <-function(df1){
box<- round(pythag.opp.leg(df1$Radius))
swc.box<- Map(function(a, b, c, d, e) data.frame(X = a:b, Y = c:d, Z = e), 
            df1$X-box, df1$X+box, df1$Y-box, df1$Y+box, df1$Z)
swc.box2<- purrr::map_df(swc.box, function(x) tidyr::expand(x, X, Y, Z))
return(swc.box2)
} 

swc_process_A<-function(x){
as.data.frame(x)->swc2
swc2[,2:6]->swc3
swc3$V2->swc3$Type
swc3$V3->swc3$X
swc3$V4->swc3$Y
swc3$V5->swc3$Z
swc3$V6->swc3$Radius
swc3[,6:10]->swc4
trunc(swc4$X)->swc4$X
trunc(swc4$Y)->swc4$Y
trunc(swc4$Z)->swc4$Z
swc4->swc5
as.data.frame((swc.fxn(swc5)))->expanded.swc.df2
return(expanded.swc.df2)
}

Attempt to apply the lapply loop 尝试应用lapply循环

`result<-lapply(df_swc_list, swc_process_A(x))`

Expected result for df_swc1 (the first df element in the list) df_swc1的预期结果(列表中的第一个df元素)

str(expanded.swc.df)

data.frame': 484 obs. data.frame':484磅。 of 3 variables: 3个变量:

X: int 100 100 100 100 100 100 100 100 100 100 ... X:整数100100100100100100100100100100 ...

Y: int -2 -1 0 1 2 3 4 5 6 7 ... Y:整数-2 -1 0 1 2 3 4 5 6 7 ...

Z: num 1 1 1 1 1 1 1 1 1 1 ... Z:num 1 1 1 1 1 1 1 1 1 1 ...

Actual error message says Error in 实际错误消息显示错误于

[.data.frame`(swc3, , 6:10) : undefined columns selected [.data.frame`(swc3,,6:10):已选择未定义的列

lapply调用应为lapply(df_swc_list, "swc_process_A")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM