/ / चरम समूह (आधार में) द्वारा स्तंभों को संकुचित करें - आर

चर (समूह में) समूहबद्ध करके कॉलम संकुचित करें - आर

मेरे पास टेक्स्ट वैरिएबल और ग्रुपिंग वैरिएबल है। मैं पाठ चर द्वारा एक स्ट्रिंग प्रति पंक्ति (गठबंधन) में टेक्स्ट वैरिएबल को पतन करना चाहता हूं। इसलिए जब तक समूह कॉलम कहता है 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

उत्तर:

जवाब के लिए 5 № 1

यह वाक्य को समूहबद्ध करने के लिए आईडी बनाने के लिए राल का उपयोग करता है। यह उत्पादन को एक साथ लाने के लिए पेस्ट के साथ tapply का उपयोग करता है

## 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])

उत्तर № 2 के लिए 1

मुझे जवाब मिला और पोस्ट पर वापस आया लेकिन डेसन ने मुझे इसे हरा दिया और खुद से ज्यादा समझ में आया।

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)

संपादित करें मैं इसे कुल मिलाकर और आपकी प्रतिक्रिया से क्या सीखा (हालांकि, एक बेहतर समाधान है):

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]])