-2
說我有這樣的數據:根據條件將多行字符串摺疊爲一行。
df <- data.frame(
text = c("Treatment1: This text is","on two lines","","Treatment2:This text","has","three lines","","Treatment3: This has one")
)
df
text
1 Treatment1: This text is
2 on two lines
3
4 Treatment2:This text
5 has
6 three lines
7
8 Treatment3: This has one
我將如何解析這個文本,以使所有的「治療」是他們自己的行與下面的所有文字在同一行?
例如,這是需要的輸出:
text
1 Treatment1: This text is on two lines
2 Treatment2: This text has three lines
3 Treatment3: This has one
誰能推薦一個辦法做到這一點?
看一看'tapply()'。它可以代替'do.call(rbind,lapply(split(...),...))' –
@RichScriven謝謝你,回答編輯你的建議。 –