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"
Related
This question already has answers here:
Count the number of all words in a string
(19 answers)
Closed 2 years ago.
I have the following string:
str1<-" india hit milestone electricity wind solar"
Number of words contained in it is:
>sapply(strsplit(str1, " "), length)
[1] 7
It is not true because we have a space at the beginning of str1. I tried to trim the white space but:
> stripWhitespace(str1) # by tm package
returns the same situation:
[1] " india hit milestone electricity wind solar"
Why?
You can just use the base function trimws
sapply(strsplit(trimws(str1), " "), length)
[1] 6
Maybe you can try
lengths(gregexpr("\\b\\w+\\b",str1))
such that
> lengths(gregexpr("\\b\\w+\\b",str1))
[1] 6
You could try using stringr::str_trim and stringr::str_split like this:
length(stringr::str_split(stringr::str_trim(str1), pattern=" ", simplify=T))
We can use str_count
library(stringr)
str_count(str1, '\\w+')
#[1] 6
This question already has answers here:
R: gsub, pattern = vector and replacement = vector
(6 answers)
Closed 3 years ago.
I have a string Vector including numbers like this:
x <- c("abc122", "73dj", "lo7833ll")
x
[1] "abc122" "73dj" "lo7833ll"
I want to Change the numbers of the x Vector and replace them with numbers I have stored in another Vector:
right_numbers <- c(500, 700, 23)
> right_numbers
[1] 500 700 23
How can I do this even if the numbers are in different positions in the string(some are at the beginning, some at the end..)?
This is how the x Vector should look like after the changes:
> x
[1] "abc500" "700dj" "lo23ll"
A vectorized solution with stringr -
str_replace(x, "[0-9]+", as.character(right_numbers))
[1] "abc500" "700dj" "lo23ll"
Possibly a more efficient version with stringi package, thanks to #sindri_baldur -
stri_replace_first_regex(x, '[0-9]+', right_numbers)
[1] "abc500" "700dj" "lo23ll"
Here is an idea,
mapply(function(i, y)gsub('[0-9]+', y, i), x, right_numbers)
# abc122 73dj lo7833ll
#"abc500" "700dj" "lo23ll"
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=" "))
This question already has an answer here:
strsplit on all spaces and punctuation except apostrophes [duplicate]
(1 answer)
Closed 7 years ago.
I'm trying to turn a character vector novel.lower.mid into a list of single words. So far, this is the code I've used:
midnight.words.l <- strsplit(novel.lower.mid, "\\W")
This produces a list of all the words. However, it splits everything, including contractions. The word "can't" becomes "can" and "t". How do I make sure those words aren't separated, or that the function just ignores the apostrophe?
We can use
library(stringr)
str_extract_all(novel.lower.mid, "\\b[[:alnum:]']+\\b")
Or
strsplit(novel.lower.mid, "(?!')\\W", perl=TRUE)
If you just want your current "\W" split to not include apostrophes, negate \w and ':
novel.lower.mid <- c("I won't eat", "green eggs and", "ham")
strsplit(novel.lower.mid, "[^\\w']", perl=T)
# [[1]]
# [1] "I" "won't" "eat"
#
# [[2]]
# [1] "green" "eggs" "and"
#
# [[3]]
# [1] "ham"
This question already has answers here:
How do I strip dollar signs ($) from data/ escape special characters in R?
(4 answers)
Closed 7 years ago.
> str = "a$b$c"
> astr <- strsplit(str,"$")
> astr
[[1]]
[1] "a$b$c"
Still trying to figure the answer out!
You need to escape it
strsplit(str,"\\$")
Another option is to use , fixed = TRUE option:
strsplit(str,"$",fixed=TRUE)
## [1] "a" "b" "c"