简体   繁体   English

使用R中的开始和停止索引创建数据帧列表

[英]Creating a list of data frames using indexes for start and stop, in R

In R, take any large data frame (example 300,000 rows and 30 columns). 在R中,采用任何大数据框(例如300,000行和30列)。 I want to create a list of data frames using start and stop index values I have stored in another data frame (two columns, first column are the start values and the second contains the stop values.) The number of rows in the start-stop df will be the number of dataframes stored in the list (in this small example, 6). 我想使用存储在另一个数据框中的开始和结束索引值创建数据帧列表(两列,第一列是起始值,第二列包含终止值。)起始-终止中的行数df将是列表中存储的数据帧数(在这个小示例中为6)。 To me there sounds like there might be an easy function to do this, but before I've always created lists of data frames before using the split command or with different conditional statements, so I did some research but couldn't find a solution. 对我来说,听起来可能有一个简单的函数可以执行此操作,但是在我始终创建数据帧列表之前,请先使用split命令或不同的条件语句,所以我进行了一些研究,但找不到解决方案。 Also, I'm double looping below, which is not preferable. 另外,我在下面循环两次,这不是可取的。 Any help greatly appreciated! 任何帮助,不胜感激!

Example of start, stop data frame 启动,停止数据帧示例

> df
     headID tailID
[1,]    688    704
[2,]   2576   2583
[3,]   4005   4018
[4,]   4336   5761
[5,]   5762   7201
[6,]   7202   8641

So I'm thinking something like (pseudo-code): 所以我在想类似(伪代码)的东西:

n <- length(bigDF)
subList <- list()
start.idx <- NA
obs <- dim(bigDF)
for(i in 2:obs){
  for(j in 1:df) {
    start.idx <- df$headID[j]
  }                                                                                    
  else if 
end.idx <- df$tailID[j]
subMat <- bigDF[start.idx:end.idx,]
subList[[counter]] <- subMat
start.idx <- NA
counter <- counter + 1
  }
}
}

I would write a function and apply it... 我会编写一个函数并将其应用...

f <- function(x, data) {
    data[x[1]:x[2],]
}

apply(df, 1, f, bigDF)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM