Convert factor into numeric values (R) [duplicate] - r

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?

Related

Conversion of dataype for multiple columns at once in R [duplicate]

This question already has answers here:
Change the class from factor to numeric of many columns in a data frame
(16 answers)
How to convert a factor to integer\numeric without loss of information?
(12 answers)
Closed 2 years ago.
so I have a csv file with around 60 variables that I have imported in R and I'd like to convert the columns to datatypes like numeric, Posixt, and Boolean. In my previous files I needed to do so only for a limited set of columns so I simply used 4-5 times.
data$var1 <- as.numeric(as.character(data$var1)
data$var2 <- as.numeric(as.character(data$var2)
For now however, when I want to convert 60 columns I'd rather not write 60 lines of code again and again. Could someone help me come up with a more efficient way?

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?

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

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.

Date Conversions in R [duplicate]

This question already has answers here:
integer data frame to date in R [duplicate]
(3 answers)
Closed 4 years ago.
My raw data file have the date columns with numbers like "180410". These are read as integers when I import the data into R. Now I tried converting them to dates using as.date but I got only N/A in these columns. Is there a way to covert them to actual dates? Please help
You need to check the format part of as.Date, it needs to match the input (2 digit year, and no spacing). If you have the wrong input you get NA. The following example produces NA because there are no hyphens in your input:
as.Date("180412", "%y-%m-%d")
Use this instead:
as.Date("180412", "%y%m%d")

as. double()/ as.numeric() not working [duplicate]

This question already has answers here:
How to convert a factor to integer\numeric without loss of information?
(12 answers)
Closed 5 years ago.
I want the program to read the data as double float but when I use as.double and as.numeric it changes the data itself.
Original data
Original data is in fractions
After applying as.double to each column separately and combining to form a dataframe, the data starts looking like this
Changed data values after applying as. double()
Your data are probably factor (not character).
To convert column x to numeric use as.numeric(levels(x))[x]
This can also help.

Resources