Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.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 基于cumsum索引重复数据帧行_R_Cumsum - Fatal编程技术网

R 基于cumsum索引重复数据帧行

R 基于cumsum索引重复数据帧行,r,cumsum,R,Cumsum,我有一个数据框,如下所示: data.frame(title="Title", bk=c("Book 1", "Book 1", "Book 3"), ch=c("Chapter 1", "Chapter 2", "Chapter 1")) title bk ch 1 Title Book 1 Chapter 1 2 Title Book 1 Chapter 2 3 Title Book 3 Chapter 1 如何根据下面的cumsum指数重复每个观察结果: id

我有一个数据框,如下所示:

data.frame(title="Title", bk=c("Book 1", "Book 1", "Book 3"), ch=c("Chapter 1", "Chapter 2", "Chapter 1"))

  title     bk        ch
1 Title Book 1 Chapter 1
2 Title Book 1 Chapter 2
3 Title Book 3 Chapter 1
如何根据下面的cumsum指数重复每个观察结果:

id=c(1,1,1,2,2,3,3,3,3)
因此,数据帧可以以这样的方式扩展,以便容纳生成cumsum索引的源向量

  title     bk        ch   source_vector
1 Title Book 1 Chapter 1   ...
1 Title Book 1 Chapter 1   
1 Title Book 1 Chapter 1   
2 Title Book 1 Chapter 2   
2 Title Book 1 Chapter 2   
3 Title Book 3 Chapter 1   
3 Title Book 3 Chapter 1   
3 Title Book 3 Chapter 1   
3 Title Book 3 Chapter 1   

一个选项是使用
分隔行

library(tidyverse)
df1 %>%
    separate_rows(content)
#  title     bk        ch content
#1 Title Book 1 Chapter 1    This
#2 Title Book 1 Chapter 1      is
#3 Title Book 1 Chapter 1     the
#4 Title Book 1 Chapter 2 content
#5 Title Book 1 Chapter 2      of
#6 Title Book 3 Chapter 1    each
#7 Title Book 3 Chapter 1 chapter
#8 Title Book 3 Chapter 1      in
#9 Title Book 3 Chapter 1   books

如果我们需要复制原始行

df1 %>% 
    uncount(str_count(content, "\\w+")) %>%
    as_tibble
# A tibble: 9 x 4
#  title bk     ch        content              
#  <fct> <fct>  <fct>     <fct>                
#1 Title Book 1 Chapter 1 This is the          
#2 Title Book 1 Chapter 1 This is the          
#3 Title Book 1 Chapter 1 This is the          
#4 Title Book 1 Chapter 2 content of           
#5 Title Book 1 Chapter 2 content of           
#6 Title Book 3 Chapter 1 each chapter in books
#7 Title Book 3 Chapter 1 each chapter in books
#8 Title Book 3 Chapter 1 each chapter in books
#9 Title Book 3 Chapter 1 each chapter in books
df1%>%
取消计数(str_计数(内容“\\w+”)%%>%
不可抵抗
#一个tibble:9x4
#标题bk ch内容
#                          
#1本书的标题1第1章这是
#2标题书1第1章这是
#3标题书1第1章这是
#4标题书1第2章目录
#5标题书1第2章目录
#6标题书3第1章书中的每一章
#7标题书3第1章书中的每一章
#8标题书3第1章书中的每一章
#9标题书3第1章书中的每一章
在base中,您可以使用
do.call的
r.bind
,在您完成每行的
strsplit
cbind
之后,如:

x <- data.frame(title="Title", bk=c("Book 1", "Book 1", "Book 3"), ch=c("Chapter 1", "Chapter 2", "Chapter 1"), content=c("This is the", "content of", "each chapter in books"))
do.call("rbind", by(x, 1:nrow(x), function(x) {cbind(x[-ncol(x)], str_split_content=strsplit(as.character(x$content[1]), " ")[[1]])}))
#    title     bk        ch str_split_content
#1.1 Title Book 1 Chapter 1              This
#1.2 Title Book 1 Chapter 1                is
#1.3 Title Book 1 Chapter 1               the
#2.1 Title Book 1 Chapter 2           content
#2.2 Title Book 1 Chapter 2                of
#3.1 Title Book 3 Chapter 1              each
#3.2 Title Book 3 Chapter 1           chapter
#3.3 Title Book 3 Chapter 1                in
#3.4 Title Book 3 Chapter 1             books

x如果您只是想根据
content
中的字数展开行,那么这里有一种方法

library(splitstackshape)
expandRows(ddf, lengths(gregexpr("\\W+", ddf$content)) + 1, count.is.col = FALSE)

#    title     bk        ch               content
#1   Title Book 1 Chapter 1           This is the
#1.1 Title Book 1 Chapter 1           This is the
#1.2 Title Book 1 Chapter 1           This is the
#2   Title Book 1 Chapter 2            content of
#2.1 Title Book 1 Chapter 2            content of
#3   Title Book 3 Chapter 1 each chapter in books
#3.1 Title Book 3 Chapter 1 each chapter in books
#3.2 Title Book 3 Chapter 1 each chapter in books
#3.3 Title Book 3 Chapter 1 each chapter in books

这更接近我想要的:

df %>%
  mutate(str_split_content = str_split(content, " ")) %>%
  unnest()
不久前有人发布了,然后修改/删除了


原来的
stru-split
内容实际上是用标点符号写的。所以不完全是按字数分开

您想如何使用
id
?或者您只是想将
内容中的每个单词分隔开以分隔行吗?原始数据是中文文本,我用
str_split
删除了其中的标点符号@akrun在我看来是一样的(要分隔的单词==组的长度),但由于不确定,我reopened@Sotos我想这和你贴的标签不一样。从那边的答案中我不需要知道什么。我重新打开了,但是我仍然看不到您想要完成什么。那么您如何处理每个id的
部分?因为如果这是解决方案,那么我们同意这是一个解决方案dupe@Sotos我想说,如果OP提出了一个巨大的
for
循环,并且想要修复一些东西,那么在没有for循环的情况下展示一个更简单的解决方案是否公平?我对你的标签的评论是基于OP帖子的意图,但他/她得到的输出是相同的。但我不明白你的意思。该示例之所以有效,是因为它们与每个组的长度相同。也许我不明白这个道理question@Sotos在这里,OP提出了一个
strsplit
,创建了ssome'id,然后希望以一种循环的方式获得预期的输出,但是,当其他人发布时,这不会发生……我看到你正在偏离友好的讨论,所以我将离开。祝你好运,阿伦@akrun我知道,但根据我们和OP的讨论,我认为他们需要了解的可能只是如何扩展……在OP澄清之前,回答假设我猜这与这个答案有什么关系?是的,我知道你不会投反对票。我不同意…是的,再加上重新打开/噪音等…但我不明白我们为什么要讨论这个…df%>%unest(str\u split\u content=str\u split(content,“”)只要阅读文档,unest允许:)