在一个现实世界的例子中,我想知道从下面创建函数的这些信息后我想要完成的事情:
list.max< - function(list,...)
其中...表示data.frames中不同的列,这些列位于列表中。
该函数将比较列中的元素,行与行,并返回一个具有最大值的向量。
为了帮助这个过程,我已经做了一些工作。这是我能得到的最接近的:
#function to return the maximum value from each line, between all the columns listed
#@Arg List: A list of data.frames which contain the columns
#@Arg col.name1 ... col.nameN: Character variable containing the names from the columns to compare
#Pre: The list must exist and the data.frames must contain the same columns
#Pos: The function will return a vector with their first element
# being the maximum value, between the columns listed, from the first
# data.frame from the list. The second element, being the maximum
# value between the columns listed, from the second data.frame from
# the list. The analogy continues until the N element
list.max <- function(list, col.name1, col.name2, ... , col.nameN){
#creates the first data.frame with the correct amount of rows
data.frame = data.frame(list.exapply(list, max, col.name1))
#loop intill the end
data.frame[1] = list.exapply(list, max, col.name1)
data.frame[2] = list.exapply(list, max, col.name2)
...
data.frame[N] = list.exapply(list, max, col.nameN)
#transpose the data.frame, so it can be compared in the max function, as he is casted to a matrix class
t(data.frame)
#creates the vector so it can storage the max value between the columns (which are now the lines)
vet = vector()
#storage the solution
for( i in 1:nrow(data.frame)) {vet[i] = max(data.frame[i,])}
#return the solution
return (vet)
}
上面使用的辅助功能是:
df.exapply <- function(data.frame, func, col.name){
variavel <-func(data.frame[, col.name])
# print(variavel)
return (variavel)
}
list.exapply <- function(list, func, col.name){
vet = df.exapply(list[[1]], func, col.name)
# print(col.name)
for (i in 1:length(list)) { vet[i] = df.exapply(list[[i]],func, col.name)
}
return (vet)
}
事先,谢谢你的帮助!
答案 0 :(得分:1)
因此,根据我收集的内容,您希望拥有一个包含x个数据帧的列表,并找到每个数据帧中所有观察值和所有变量的最大值。 你为什么不这样做:
# Create list with 10 dataframes
df_list <- list()
for (i in 1:10) {
df_list[[i]] <- data.frame(matrix(rnorm(100), ncol = 10))
colnames(df_list[[i]]) <- LETTERS[1:10]
}
# Find maximum value of all data.frames
sapply(df_list, FUN = max)
这将创建一个包含10个数据帧的列表,每个数据帧包含10个观察值和10个变量。然后它遍历每个data.frame以获得每个data.frame的最大值。最后,返回具有最大值的向量。