Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:有条件地将列表项添加到以前的列表项_R_Algorithm_List_Loops - Fatal编程技术网

R:有条件地将列表项添加到以前的列表项

R:有条件地将列表项添加到以前的列表项,r,algorithm,list,loops,R,Algorithm,List,Loops,我一直在尝试使用R将pdf文件转换为数据帧。我首先将文本读入R,然后使用data.table将数据拆分为每页的列表项。我现在很难写一个循环来将这些问题与它们各自的续项结合起来。下面代码中的txt.list对象是该格式的一个简单示例 ### Short list txt.list <- list('Q1', 'Q2', 'continued page', 'Q3', 'continued page', 'continued page',

我一直在尝试使用R将pdf文件转换为数据帧。我首先将文本读入R,然后使用data.table将数据拆分为每页的列表项。我现在很难写一个循环来将这些问题与它们各自的续项结合起来。下面代码中的txt.list对象是该格式的一个简单示例

### Short list
txt.list <- list('Q1', 'Q2', 'continued page', 
                 'Q3', 'continued page', 'continued page', 
                 'Q4', 'Q5', 'continued page', 'continued page', 
                 'Q6', 'continued page', 'Q7', 'continued page', 'continued page')

### Label pages that continue from the previous
is.continuation <- lapply(txt.list, function(x){ startsWith(x, 'continued')}) # find which pages are continuations
is.continuation <- c(unlist(is.continuation)) # unlist for list item naming

names(txt.list) <- as.character(is.continuation)

print(txt.list)
这个结果是,列表中的每一页,作为相应问题的延续,都有一个真正的字符标签,我知道这可以不用列表标签来完成,我只是试图避免引用外部向量

因为这个网站上的每个pdf文件几乎都使用相同的格式,所以我试图让它至少在某种程度上为将来的使用服务。我一直在尝试以下几点:

new.list <- vector(mode = 'list', 
                   length = length(which(names(txt.list) == 'TRUE')))

for(i in 1:length(txt.list)){
  j = i + 1 # pg ahead

  if(names(txt.list)[[j]] == "TRUE"){

    new.list[[i]][[1]] <- txt.list[[i]]
    m = 2 # index ahead

    while(names(txt.list)[[j]] == "TRUE"){
      new.list[[i]][[m]] <- txt.list[[j]]
      m = m + 1
    }
  } else {
      new.list[[i]] <- txt.list[[i]]
  }
}

试了几次之后,我就完全画空白了。任何帮助都将不胜感激

我已经有一段时间没有真正在r工作了,但我是否误读了你的for循环?你不需要我在1:长度。。。?如果没有1:part,则没有范围,因此不会进行任何循环

除此之外,您的主要问题是在“i”位置输入新列表,而该变量仅适用于从txt.list读取。您应该为new.list(如nlSize)保留一个单独的跟踪器,并在适当的时候勾选它

另一个小问题是,在while循环之前有一个可以避免的锚点

最后,我肯定不会将名称设置为真值。最好是引用外部向量,尽管也不必这样做。 只需创建一个函数并在循环中使用它

我将代码放在一个名为normalizeList的函数中,然后在txt.list上调用它。这样,您就可以在其他类似列表中使用它

normalizeList <- function (lst) {

    is.continuation <- function (x) 
        startsWith(x, 'continued');

    new.list <- list()
    nlSize <- 0

    for(i in 1:length(lst)) {    

      isLast <- length(lst) == i
      cur <- lst[[i]]
      nxt <- ifelse(isLast, '', lst[[i+1]]);

      if(is.continuation(cur)){
        new.list[[nlSize]] <- c(new.list[[nlSize]], cur)
        next
      } 

      nlSize <- nlSize + 1
      new.list[nlSize] <- ifelse(is.continuation(nxt), list(cur), cur)

    }

    new.list

}

normalizeList(txt.list);

非常感谢,非常好的解释!但是,是的,我的第二段代码很快就从我的脑海中消失了。意味着1:如果有帮助,考虑投票并接受答案。祝你好运