我有一个数据框,其中包含一列和这样的行:
ROW1:
something here
another line here
and we are here
but we also have this
ROW2:
something here2
another line here2
and we are here2
but we also have this2
是否可以删除大空格并将所有文本拼接成一行?输出是这样的:
row1: something here another line here and we are here but we also have this
row2: something here2 another line here2 and we are here2 but we also have this2
答案 0 :(得分:0)
尝试在行上执行此操作以从字符串中删除回车符和新行:
library(stringr)
str_replace_all(x, "[\r\n]" , "")
x代表你的字符串
答案 1 :(得分:0)
看起来您想要将所有空白区域折叠到一个空间中。从这个SO问题(Merge Multiple spaces to single space; remove trailing/leading spaces)得到的结果应该给出了预期的结果:
string<-"something here2
another line here2
and we are here2
but we also have this2
"
library(stringr)
gsub("\\s+"," ",str_trim(string))
##[1] "something here2 another line here2 and we are here2 but we also have this2"
对于数据框:
df<-structure(list(strings = structure(c(2L, 1L), .Label = c("something here\n\nanother line here\n\n\n\nand we are here\n\n\nbut we also have this\n",
"something here2\n\nanother line here2\n\n\n\nand we are here2\n\n\nbut we also have this2\n"
), class = "factor"), strings_cl = c("something here2 another line here2 and we are here2 but we also have this2",
"something here another line here and we are here but we also have this"
)), .Names = c("strings", "strings_cl"), row.names = c(NA, -2L
), class = "data.frame")
df$strings_cl <- gsub("\\s+"," ",str_trim(df$strings))