Remove Characters in R [duplicate] - r

This question already has answers here:
Replace all particular values in a data frame
(8 answers)
How do I deal with special characters like \^$.?*|+()[{ in my regex?
(2 answers)
Closed 4 years ago.
I have a large data frame with a character value in various columns and rows. The character is [subnet].
I am trying to get rid of it by writing the following code
new_data[]=lapply(new_data, gsub, pattern="[subnet]", replacement='')
However, it seems like although "subnet" is disappearing, I am ending up with
"[]" for every instance of the character.
Any idea or fix would be appreciated.

Related

r comparison operator is not working for a very simple code [duplicate]

This question already has answers here:
Dealing with spaces and "weird" characters in column names with dplyr::rename()
(1 answer)
R dplyr filter column with column name that starts with number
(1 answer)
Closed 2 years ago.
The filter in the first picture is clearly not working. None of the rows are filtered. However, in the second picture, there are clearly much less rows that satisfy the condition average value < good(blue) than the first picture.
Why is the code in the first picture not working and what may be a possible solution?

Replace last characters of a string with its entire elements [duplicate]

This question already has answers here:
Extracting the last n characters from a string in R
(15 answers)
Closed 3 years ago.
I have an element in my dataframe which I want to modify.
I have a column with the following type of values
https://mns-xyz-eu.abc.com/ccs/proposal?action=view&proposalId=12345
I want to replace the entire string with just the last 5 characters (i.e)
Replace the entire character string with 12345 in this case.
How do I achieve this?
Thanks a lot.
One option is using a positive look behind using stringr::str_extract
str_extrct('https://mns-xyz-eu.abc.com/ccs/proposal?action=view&proposalId=12345',
'(?<=proposalId\\=)\\d+')
#Simple option
str_extract('https://mns-xyz-eu.abc.com/ccs/proposal?action=view&proposalId=12345', '\\d+')

Extract information from string with dots as separator in R [duplicate]

This question already has answers here:
How to use the strsplit function with a period
(3 answers)
Closed 5 years ago.
I apologize for possible similar questions, but I just can't find the solution for my problem. So, I have a string with three parts, separated by “.”, for example:
a <- "XXX.YY.ZZZ"
(the length of strings differ, it could also be a <- "XXXX.Y.ZZ", but the three parts are always separated by the two “.”.
I solved the problem for the first part:
library(stringi)
stri_extract(a, regex='[^.]*')
[1] "XXX"
Appreciate your help.
hello you can use strsplit as follows
strsplit(a,"\\.")[[1]]

creating string of numbers seperated by commas [duplicate]

This question already has answers here:
Creating a comma separated vector
(6 answers)
Closed 6 years ago.
I am trying to create a comma separated string exactly like this to put in a url: 1,2,3,4,5 but when I try: paste0(1:5,sep=",") it returns: [1] "1," "2," "3," "4," "5," The spaces are causing issues. How do I get rid of them?
This is not the same question as: this SO post the solution is almost the same, but the question approaches it from a different angle. My question is trying to suppress the spaces, the linked question wants them.
Try to use collapse="," instead of sep=",":
paste0(1:5,collapse=",")
[1] "1,2,3,4,5"

R displays numbers in scientific notation [duplicate]

This question already has answers here:
How can I paste 100000 without it being shortened to 1e+05? [duplicate]
(3 answers)
Closed 9 years ago.
The result of function was displayed in scientific notation, I want to change it back to normal, but only for that function, I don't want to change the global setting. Can anyone help?
You can do:
format(functionResult, scientific=FALSE);
or:
as.integer(functionResult);

Resources