This question already has answers here:
Replace specific characters within strings
(7 answers)
Closed 7 years ago.
In my data.frame, I have a column of type character, where all the values look like this : 123_456 (three digits, an underscore, three digits).
I need to transform these values to a numeric, and as.numeric(my_dataframe$my_column) gives me a NA. Therefore I need to remove the underscore first, in order to do as.numeric.
How would I do that please ?
Thanks
We can use sub
as.numeric(sub("_", "", my_dataframe$my_column))
Related
This question already has answers here:
Getting and removing the first character of a string
(7 answers)
Remove prefix letter from column variables
(3 answers)
Closed 2 years ago.
I've got a data with column names that look like this:
X121.10.21 X131.90.23
I want to remove the X at the beginning of each string, remove the third number after the . and then reorder the first and second number. Like this:
10.121 90.131
How can I do this? I would especially appreciate a way to do this with dplyr, if possible.
We can use sub, capture as a group and replace with the backreference of the captured group
names(df1) <- sub("X(\\d+)\\.(\\d+)\\..*", "\\2.\\1", names(df1))
This question already has answers here:
Exclude everything after the second occurrence of a certain string
(2 answers)
Closed 3 years ago.
I have a vector which has names of the columns
group <- c("amount_bin_group", "fico_bin_group", "cltv_bin_group", "p_region_bin")
I want to replace the part after the second "_" from each element i.e. I want it to be
group <- c("amount_bin", "fico_bin", "cltv_bin", "p_region")
I can split this into two vectors and try gsub or substr. However, it would be nice to do that in vector. Any thoughts?
I checked other posts regarding the same question, but none of them has this framework
> sub("(.*)_.*$", "\\1", group)
[1] "amount_bin" "fico_bin" "cltv_bin" "p_region"
This question already has answers here:
Convert currency with commas into numeric
(4 answers)
removing particular character in a column in r
(3 answers)
Closed 4 years ago.
I am new to R. I am working on a dataset which is a large list, there is a column with numbers and strings. I want to remove the string and convert to numeric.
I have 100,000+
I want 100,000 and numeric
You can use gsub to transform the string:
as.numeric(gsub("[^0-9.]", "", "100,000+"))
# [1] 1e+05
Here, all characters, except digits and ., are removed before applying as.numeric.
This question already has answers here:
Regex to check whether a string contains only numbers [duplicate]
(21 answers)
Check if string contains ONLY NUMBERS or ONLY CHARACTERS (R)
(3 answers)
Closed 4 years ago.
I'm wondering what regex pattern I could use to detect if every character in a string is numeric.
So, here's what I'm thinking that isn't working:
stringr::str_detect("311apple", "[0-9]")
#> [1] TRUE
That statement is TRUE because there exists a numeric character in that string, but I'm trying to find a pattern so that it's only TRUE if every character is numeric.
Any ideas? Thanks!
This question already has answers here:
pad numeric column with leading zeros
(2 answers)
Closed 4 years ago.
I want to convert a character column into numeric column in a data frame.
The column values are like "0001" "0002"...
I used
as.numeric(as.character(column_name))
it returns 1,2...
but the leading zeros are removed.
I want to keep the leading zeros and change column's type.
If you want leading zeroes, you do not have the option to make it numeric. A numeric class is by definition, logically, numeric, which means leading zeroes would not have any meaning. So you need to decide whether you like to keep it as a character column, OR to convert it to numeric and accept that that leads to a deletion of the leading zeroes.