This question already has answers here:
Removing a group of words from a character vector
(2 answers)
Closed 5 years ago.
I think the title is a bit confusing, but here my problem:
I have 2 vectors, one containing some text the other one containing some phrases
text <- c("this is some text","some elements should be removed", "i hope you can help me with this text element problem")
pattern <- c("text", "some","be")
And now I want to remove all elements from patternwhich are in text, so as result vector
text_result
[1] "this is"
[2] "elements should removed"
[3] "i hope you can help me with this element problem"
I tried
text_result <- sapply(pattern, function(x) gsub(x, text, replacement =""))
or
text_result <- sapply(text, function(y) sapply(pattern, function(x)gsub(x,y,replacement ="")))
but in both cases I receive a large matrix with
length(pattern)*length(text) elements
thanks in advance!
You can try:
`%notin%` <- function(x,y) !(x %in% y)
lapply(strsplit(text," "),function(x) paste(x[x %notin% pattern],collapse=" "))
Related
This question already has an answer here:
regular expression match digit and characters
(1 answer)
Closed 2 years ago.
I need an additional solution to the previous question/answer
Move characters from beginning of column name to end of column name
I have a dataset where column names have two parts divided by _ e.g.
pal036a_lon
pal036a_lat
pal036a_elevation
I would like to convert the prefixes into suffixes so that it becomes:
lon_pal036a
lat_pal036a
elevation_pal036a
The answer to the previous question
names(df) <- sub("([a-z])_([a-z]+)", "\\2_\\1", names(df))
does not work for numbers within the prefixes.
Assuming your names have a single _. You could also you strsplit():
sapply(strsplit(names(df), '_'), function(x) paste(rev(x), collapse = '_'))
If you have more than one you could modify the above as suggested by jay.sf:
sapply(strsplit(x, "_"), function(x) paste(c(x[length(x)], x[-length(x)]), collapse="_"))
You can include alphanumeric characters in the first group:
names(df) <- sub("([a-z0-9]+)_([a-z]+)", "\\2_\\1", names(df))
For example :
x <- c("pal036a_lon","pal036a_lat","pal036a_elevation")
sub("([a-z0-9]+)_([a-z]+)", "\\2_\\1",x)
#[1] "lon_pal036a" "lat_pal036a" "elevation_pal036a"
This question already has an answer here:
R how to not display the number into brackets of the row count in output
(1 answer)
Closed 2 years ago.
x <- 5+2
print(x)
[1] 7
How to suppress [1] and only print 7?
Similarly for characters:
y <- "comp"
print(y)
[1] "comp"
I want to remove both [1] and " ". Any help is appreciated!
Thanks!
With cat, it is possible
cat(x, '\n')
7
Or for characters
cat(dQuote(letters[1], FALSE), '\n')
"a"
This question already has answers here:
How to remove all whitespace from a string?
(9 answers)
Closed 1 year ago.
I want to merge following two strings in R (and remove the spaces). I was using paste but I was not able to get desired results.
a <- "big earth"
b <- "small moon"
c <- paste(a,b, sep = "")
I want to have a c <- "bigearthsmallmoon"
Thank you very much for the help.
You can paste the strings together into one with paste(). Then you can use gsub() to remove all spaces:
gsub(" ", "", paste(a, b))
# [1] "bigearthsmallmoon"
c <- paste(a, b, sep = "")
This question already has answers here:
Filter rows which contain a certain string
(5 answers)
Closed 3 years ago.
Using this command it keeps the rows which have the specific word
df[df$ID == "interesting", ]
If this word is exist in the row but it has more words around how is it possible to find if this word exist and keep the row.
Example input
data.frame(text = c("interesting", " I am interesting for this", "remove")
Expected output
data.frame(text = c("interesting", " I am interesting for this")
1.Example data:
df <- data.frame(text = c("interesting", " I am interesting for this", "remove"),
stringsAsFactors = FALSE)
Solution using base R. Indexing using grepl:
df[grepl("interesting", df$text), ]
This returns:
[1] "interesting" " I am interesting for this"
Edit 1
Change code so that it returns a data.frame and not a vector.
df[grep("interesting", df$text), , drop = FALSE]
This now returns:
text
1 interesting
2 I am interesting for this
This question already has an answer here:
element-wise concatenation of string vectors [duplicate]
(1 answer)
Closed 3 years ago.
I would like to have a list where each item is "Born in " each of the cities of the cities vector.
cities <- c("Lausanne", "Zurich", "Geneva")
mylist <- list()
for (i in 1:length(cities)){
for (city in cities){
born <- paste0("Born in ", city)
mylist[[i]] <- born
}
}
I need it in the list format because I have much more complex data and would actually be adding dataframes into the list. I would like to understand how the double loop would work (one for items and one for length).
Thank you!
If you want to put the data in list you can use as.list
as.list(paste0("Born in ", cities))
#[[1]]
#[1] "Born in Lausanne"
#[[2]]
#[1] "Born in Zurich"
#[[3]]
#[1] "Born in Geneva"
If you really want to use for loop, you'll need only one loop
mylist <- vector("list", length(cities))
for (i in seq_along(cities)) {
mylist[[i]] <- paste0("Born in ", cities[i])
}