I have a list of data frames which I need to obtain the last row of the 2nd column from. All the data frames have differing number of rows. I've already written code using lapply which can extract any row by variable "num" (returning NA for numbers which exceed the row length of the data frames) , however I want to include a variable num="worst" which will return the last row, 2nd column of available data. This is the code to retrive the "nth" row (xyz is the list of data frames):
我有一个数据框列表,我需要从中获取第二列的最后一行。所有数据帧都有不同的行数。我已经使用lapply编写了代码,它可以通过变量“num”提取任何行(对于超过数据帧行长度的数字返回NA),但是我想要包含一个变量num =“worst”,它将返回最后一行,第二列可用数据。这是用于检索“第n”行的代码(xyz是数据帧列表):
if(num=="best"){num=as.integer(1)} else
(num=as.integer())
rownumber<-lapply(xyz, "[", num, 2, drop=FALSE)
Been cracking my head all day trying to find a solution to declare num=="worst". I want to avoid loops hence my use of lapply, but perhaps there is no other way?
一整天都在试图找到一个声明num ==“最差”的解决方案。我想避免循环,因此我使用lapply,但也许没有其他方法?
2 个解决方案
#1
2
My understanding of the question is that you want a function that returns the second column of a data.frame
from a list
of dataframes, with an optional argument worst
that allows you to restrict it to the last observation.
我对这个问题的理解是你需要一个从数据帧列表中返回data.frame第二列的函数,其中一个可选参数最差,允许你将它限制在最后一个观察点。
I think the siimplest way to do this is to write a helper function, and then apply it to your list using lapply
.
我认为最简单的方法是编写辅助函数,然后使用lapply将其应用到列表中。
I have written a selector
function that takes a row and column argument, as well as a worst
argument. I think this does everything you need.
我编写了一个选择器函数,它接受一个行和列参数,以及一个最差的参数。我认为这可以满足您的一切需求。
df1 <- data.frame(A = rnorm(10), B = rnorm(10), C = rnorm(10))
df2 <- data.frame(A = rnorm(10), B = rnorm(10), C = rnorm(10))
ldf <- list(df1, df2)
selector <- function(DF, col, row=NULL, worst=FALSE){
if(!is.null(row)) return(DF[row, col])
if(!missing("col")) if(col > ncol(DF)) return(NA)
if(!is.null(row)) if(row > nrow(DF)) return(NA)
if(worst) {
tail(DF[,col, drop=F],1)
} else {
DF[row, col, drop=FALSE]
}
}
lapply(ldf, selector, worst=T)
#2
14
How about...
lapply(xyz, tail, 1)
#1
2
My understanding of the question is that you want a function that returns the second column of a data.frame
from a list
of dataframes, with an optional argument worst
that allows you to restrict it to the last observation.
我对这个问题的理解是你需要一个从数据帧列表中返回data.frame第二列的函数,其中一个可选参数最差,允许你将它限制在最后一个观察点。
I think the siimplest way to do this is to write a helper function, and then apply it to your list using lapply
.
我认为最简单的方法是编写辅助函数,然后使用lapply将其应用到列表中。
I have written a selector
function that takes a row and column argument, as well as a worst
argument. I think this does everything you need.
我编写了一个选择器函数,它接受一个行和列参数,以及一个最差的参数。我认为这可以满足您的一切需求。
df1 <- data.frame(A = rnorm(10), B = rnorm(10), C = rnorm(10))
df2 <- data.frame(A = rnorm(10), B = rnorm(10), C = rnorm(10))
ldf <- list(df1, df2)
selector <- function(DF, col, row=NULL, worst=FALSE){
if(!is.null(row)) return(DF[row, col])
if(!missing("col")) if(col > ncol(DF)) return(NA)
if(!is.null(row)) if(row > nrow(DF)) return(NA)
if(worst) {
tail(DF[,col, drop=F],1)
} else {
DF[row, col, drop=FALSE]
}
}
lapply(ldf, selector, worst=T)
#2
14
How about...
lapply(xyz, tail, 1)