【问题标题】:Collapse columns by grouping variable (in base)通过分组变量折叠列(在基础中)
【发布时间】:2012-03-25 03:49:18
【问题描述】:

我有一个文本变量和一个分组变量。我想将文本变量按因子折叠成每行(组合)一个字符串。所以只要分组栏说m我想把文字分组在一起等等。我提供了前后的样本数据集。我正在为一个包写这个,到目前为止,我已经避免了对除wordcloud之外的其他包的所有依赖,并希望保持这种方式。

我怀疑rle 可能对cumsum 有用,但我无法弄清楚这一点。

提前谢谢你。

数据是什么样子的

                                 text group
1       Computer is fun. Not too fun.     m
2               No its not, its dumb.     m
3              How can we be certain?     f
4                    There is no way.     m
5                     I distrust you.     m
6         What are you talking about?     f
7       Shall we move on?  Good then.     f
8 Im hungry.  Lets eat.  You already?     m

我希望数据是什么样的

                                                       text group
1       Computer is fun. Not too fun. No its not, its dumb.     m
2                                    How can we be certain?     f
3                          There is no way. I distrust you.     m
4 What are you talking about? Shall we move on?  Good then.     f
5                       Im hungry.  Lets eat.  You already?     m

数据

dat <- structure(list(text = c("Computer is fun. Not too fun.", "No its not, its dumb.", 
"How can we be certain?", "There is no way.", "I distrust you.", 
"What are you talking about?", "Shall we move on?  Good then.", 
"Im hungry.  Lets eat.  You already?"), group = structure(c(2L, 
2L, 1L, 2L, 2L, 1L, 1L, 2L), .Label = c("f", "m"), class = "factor")), .Names = c("text", 
"group"), row.names = c(NA, 8L), class = "data.frame")

编辑:我发现我可以为组变量的每次运行添加唯一列:

x <- rle(as.character(dat$group))[[1]]
dat$new <- as.factor(rep(1:length(x), x))

产量:

                                 text group new
1       Computer is fun. Not too fun.     m   1
2               No its not, its dumb.     m   1
3              How can we be certain?     f   2
4                    There is no way.     m   3
5                     I distrust you.     m   3
6         What are you talking about?     f   4
7       Shall we move on?  Good then.     f   4
8 Im hungry.  Lets eat.  You already?     m   5

【问题讨论】:

    标签: r


    【解决方案1】:

    这利用 rle 创建一个 id 来对句子进行分组。它使用 tapply 和 paste 将输出组合在一起

    ## Your example data
    dat <- structure(list(text = c("Computer is fun. Not too fun.", "No its not, its dumb.", 
    "How can we be certain?", "There is no way.", "I distrust you.", 
    "What are you talking about?", "Shall we move on?  Good then.", 
    "Im hungry.  Lets eat.  You already?"), group = structure(c(2L, 
    2L, 1L, 2L, 2L, 1L, 1L, 2L), .Label = c("f", "m"), class = "factor")), .Names = c("text", 
    "group"), row.names = c(NA, 8L), class = "data.frame")
    
    
    # Needed for later
    k <- rle(as.numeric(dat$group))
    # Create a grouping vector
    id <- rep(seq_along(k$len), k$len)
    # Combine the text in the desired manner
    out <- tapply(dat$text, id, paste, collapse = " ")
    # Bring it together into a data frame
    answer <- data.frame(text = out, group = levels(dat$group)[k$val])
    

    【讨论】:

    • 我不相信你需要 "seq(length(k$len))" 因为序列将 "seq_along" k$length 向量,给你等效的数字序列:id
    • @BryanGoodrich 很好。最初我只是打算做 1:length(k$len) 但最近我更倾向于使用 seq 和 seq_along 我想我最终把这两种方法搞混了。
    • 我通常只坚持使用 seq,但为了清楚起见,我可以看到 seq_along 如何明确表明您正在以数字方式遍历值向量。当我使用 x[which(...some logic here...)] 处理布尔向量上的冗余时,我经常倾向于采用清晰的路线。这不是必需的,但它确实为我喜欢的编码提供了语言清晰度。
    【解决方案2】:

    我得到了答案并回来发帖,但 Dason 打败了我,而且比我自己的更容易理解。

    x <- rle(as.character(dat$group))[[1]]
    dat$new <- as.factor(rep(1:length(x), x))
    
    Paste <- function(x) paste(x, collapse=" ")
    aggregate(text~new, dat, Paste)
    

    编辑 我将如何使用聚合以及我从您的回复中学到的东西(尽管 tapply 是一个更好的解决方案):

    y <- rle(as.character(dat$group))
    x <- y[[1]]
    dat$new <- as.factor(rep(1:length(x), x))
    
    text <- aggregate(text~new, dat, paste, collapse = " ")[, 2]
    data.frame(text, group = y[[2]])
    

    【讨论】:

    • 请注意,您不需要定义“粘贴”,因为聚合允许您将其他参数传递给正在应用的函数。您应该能够删除粘贴并改用它aggregate(text ~ new, dat, paste, collapse = " ")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-02-07
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    相关资源
    最近更新 更多