Convert string to numeric in a list in R [duplicate] - r

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.

Related

R character string methods [duplicate]

This question already has answers here:
How to calculate the number of occurrence of a given character in each row of a column of strings?
(14 answers)
Count the number of pattern matches in a string
(6 answers)
Closed 2 years ago.
Say I have a strings
seq1 <- "ACTACTGGATGACT"
pattern1 <- "ACT"
What is the best way to find the number of times the pattern is in the sequence, in R? I would like to use a sliding window for loop, but im not clear on the proper way to handle the character strings.
We can use str_count
library(stringr)
str_count(seq1, pattern1)
#[1] 3

Convert factor into numeric values (R) [duplicate]

This question already has answers here:
How to convert a factor to integer\numeric without loss of information?
(12 answers)
as.numeric with comma decimal separators?
(7 answers)
Closed 3 years ago.
I´m sure this is simple, but I couldn´t find the right answer to my specific problem.
I have a data frame in R. The first column has a DateTime (POSIXct) format.
The values in all the other columns have the format "factor".
Since all of the values in these columns are numbers I want to convert them in to a numeric format.
An additional problem is that since I´m from europe the numbers (factors) have "," as a decimal mark.
How do I convert these columns in to a numeric format?

r as.numeric change big values even with as.character [duplicate]

This question already has answers here:
How to work with large numbers in R?
(1 answer)
Preserving large numbers
(8 answers)
Exponentiate very large numbers in R
(3 answers)
Closed 4 years ago.
I'm struggling to convert some big numbers to numeric in R while using "as.numeric"; even when combining it with as.character.
I have a character class column whose values are all numeric. To be specfic, 23 numbers in each value.
While trying to covert the whole column to numeric, the values are changing, and even if I try to do it just with one value I have the same problem (in numeric or character format)
that's what I've being trying:
as.numeric(as.character(20161128000215091389073))
as.numeric(as.character("20161128000215091389073"))
as.numeric(20161128000215091389073)
or
as.numeric(as.character("20161226000750155832528"))
as.numeric(as.character(20161226000750155832528))
as.numeric(20161226000750155832528)
returns respectively:
20161128000215089938222
and
20161226000750157496020
I couldn't make it return the same number... any ideas?

R How do i split a string into a vector so that each place in the vector corresponds to a letter [duplicate]

This question already has answers here:
Split a character vector into individual characters? (opposite of paste or stringr::str_c)
(4 answers)
Closed 4 years ago.
I am trying to split a string let's say "abcde" into a vector of "a","b","c","d","e"
How can i do that?
i have tried strsplit but that makes it into 1 element
a=unlist(strsplit("abcde", split=" "))
We need the split = ""
unlist(strsplit('abcde', ''))

Remove underscore from a string in R [duplicate]

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))

Resources