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

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.

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?

Select only numeric variables of a data frame in R [duplicate]

This question already has answers here:
Why does apply convert logicals in data frames to strings of 5 characters?
(2 answers)
Selecting only numeric columns from a data frame
(12 answers)
Closed 2 years ago.
I know that the question is very easy, but I have a more specific one:
I have a data frame, with 50 variables (numeric and non-numeric) and 5000 observations.
Now what I want to do is create another data frame containing only the numerica variables of the original one.
On this website I found the solution of my problem, that is:
numeric_variables<-unlist(lapply(original_data,is.numeric))
X<-original_data[numeric_variables]
But I was wondering: why if I try like this, it does not work instead? what's wrong?
numeric_variables2<-apply(original_data,2,is.numeric)
x<-original_data[numeric_variables2]
try this :
names_num <- names(which(sapply(df, is.numeric)))
df_num <- df[, names_num]

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?

How to format numbers as percentage values but to still be able to treat ir numercially? [duplicate]

This question already has answers here:
How to format a number as percentage in R?
(10 answers)
Represent numeric value with typical dollar amount format
(4 answers)
Closed 5 years ago.
I want to know if it's possible to display values on a table as percentages but still be able to do arithmetic operations with them.
I have tried using percent() from scales package, but it seems like it transforms the values from numeric to character. I have tried to convert them back using as.numeric(), but it will not work either.
Any solutions? Thank you.
One very hacky way to do this would be as follows
x <- runif(10)
class(x) <- "percent"
print.percent <- function(x) print(scales::percent(as.numeric(x)))
This is probably useful for quick analyses or short scripts but I wouldn't put this into any kind of package or shared code.

Convert factor to integer [duplicate]

This question already has answers here:
How to convert a factor to integer\numeric without loss of information?
(12 answers)
Closed 6 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I am manipulating a data frame using the reshape package. When using the melt function, it factorizes my value column, which is a problem because a subset of those values are integers that I want to be able to perform operations on.
Does anyone know of a way to coerce a factor into an integer? Using as.character() will convert it to the correct character, but then I cannot immediately perform an operation on it, and as.integer() or as.numeric() will convert it to the number that system is storing that factor as, which is not helpful.
Thank you!
Jeff
Quoting directly from the help page for factor:
To transform a factor f to its original numeric values, as.numeric(levels(f))[f] is recommended and slightly more efficient than as.numeric(as.character(f)).
You can combine the two functions; coerce to characters thence to numerics:
> fac <- factor(c("1","2","1","2"))
> as.numeric(as.character(fac))
[1] 1 2 1 2

Resources