我已经创建了一个函数,它根据两个不同列的一些唯一值来过滤数据帧。我尝试遍历单元格类型(3)的唯一值以及另一个名为Cell_line(2)的列中的唯一值。我已经创建了两个列表来保存此信息,并使用嵌套循环对每个列表进行计数。New Dataframe似乎是每个列表(cell_line和types)的最后一次迭代,并省略了其他输出。我如何也能获得这些。数据帧列表或将所有信息绑定在一起的单个数据帧都可以工作
### My function takes a few arguments and gives a new dataframe
myfunction <- function(data_frame,type,Cell) {
prism <- df2%>%
ungroup() %>%
filter(.,TYPE == type & Cell_Line == Cell) %>%
pivot_wider(., id_cols = c("Treatment_rep","value","lipid"),
names_from = Treatment_rep, values_from = value)
prism$Cell_line <- Cell
prism
}
### I'm attempting to feed these into my function iteratively
cell_lines <- unique(df2$Cell_Line) ## list of 3 types
types <- unique(df2$TYPE) ### list of 2 types
### nested loop
for (i in 1:length(types)) {
for(j in 1: length(cell_lines)) {
newdf <- myfunction(data_frame = df2, type = types[i], Cell = cell_lines[j])
}
}
发布于 2020-03-19 17:08:23
你可以做到的
dflist <- list()
for (i in 1:length(types)){
for(j in 1: length(cell_lines)){
newdf <- myfunction(data_frame = df2, type = types[i], Cell = cell_lines[j])
dflist[[ length(dflist)+1 ]] <- newdf
}
}
如果之后你想把它们都绑定在一起
df_total <- do.call(rbind, datalist)
https://stackoverflow.com/questions/60761657
复制