我创建了一个函数,该函数基于2个不同列的某些唯一值来过滤数据框。我试图遍历单元格类型(3)的唯一值以及另一列Cell_line(2)的唯一值。我创建了两个列表来保存此信息,并且正在使用嵌套循环逐个计数。新数据框似乎是每个列表(cell_line和类型)的最后一次迭代,并且省略了其他输出。我也如何获得这些。数据框列表或将所有信息绑定在一起的单个数据框都可以工作
### 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])
}
}
答案 0 :(得分:1)
您可以这样做
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)