Recolher colunas agrupando a variável (na base)
Tenho uma variável de texto e uma variável de agrupamento. Gostaria de recolher a variável de texto em uma string por linha (combinar) por fator. Então, desde que a coluna do grupo digam
Quero agrupar o texto e assim por diante. Forneci um conjunto de dados de amostra antes e depois. Estou escrevendo isso para um pacote e até agora tenho evitado toda a dependência de outros pacotes, excetowordcloud
e gostaria de mantê-lo assim.
Suspeitorle
pode ser útil comcumsum
mas não consegui descobrir isso.
Agradeço antecipadamente
Como são os dados
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
Como eu gostaria que os dados fossem
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
Os dado
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")
EDITAR Descobri que posso adicionar uma coluna exclusiva para cada execução da variável de grupo com:
x <- rle(as.character(dat$group))[[1]]
dat$new <- as.factor(rep(1:length(x), x))
Produzindo
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