user3604060
user3604060

Reputation: 1

Replacing strings in R

I am trying to replace strings in R in a large number of texts.

Essentially, this reproduces the format of the data from which I try to delete the '\n' parts.

document <- as.list(c("This is \\na try-out", "And it \\nfails"))

I can do this with a loop and gsub but it takes forever. I looked at this post for a solution. So I tried: temp <- apply(document, 2, function(x) gsub("\\n", " ", fixed=TRUE)). I also used lapply, but it also gives an error message. I can't figure this out, help!

Upvotes: 0

Views: 1149

Answers (1)

David Arenburg
David Arenburg

Reputation: 92292

use lapply if you want to return a list

document <- as.list(c("This is \\na try-out", "And it \\nfails"))
temp <- lapply(document, function(x) gsub("\\n", " ", x, fixed=TRUE))
##[[1]]
##[1] "This is  a try-out"

##[[2]]
##[1] "And it  fails"

Upvotes: 1

Related Questions