我有一个数据框列表(subspec2),我想循环遍历这些数据框以从每个数据框中获取具有最大值的列,并将它们写入新的数据框。我编写了以下循环:
good.data<-data.frame(matrix(nrow=401, ncol=78)) #create empty dataframe
for (i in length(subspec2)) ##subspec2 is the list of dataframes
{
max.name<-names(which.max(apply(subspec2[[i]],MARGIN=2,max))) #find column name with max value
good.data[,i]<-subspec2[[i]][max.name] #write the contents of this column into dataframe
}
Run Code Online (Sandbox Code Playgroud)
这似乎可行,但仅返回最后一列中的值,其他似乎没有保存。许多线程指出df必须在循环之外,但这不是这里的问题。
我究竟做错了什么?
谢谢!