Home > OS >  Changing data type from character to numeric
Changing data type from character to numeric

Time:11-18

I have a data frame called cds in excel that consist in several prices over time as follows: enter image description here

The main problem is that when I export the data, R treat the prices as characters, so I cannot run time series commands over the data.

enter image description here

I already try the argument col_types in the read_excel function, but the problem is that consider the first date column as a number and not date format as it should.

I already try as well the as.numericcommand, however it shrink the hole data frame into a simple vector.

How can I solve this issue?

CodePudding user response:

Try type.convert():

library(dplyr)

result <- cds %>% 
  type.convert(as.is = TRUE)

result

CodePudding user response:

You can use dplyr::mutate for this:

## make some fake data ##
mtcars$mpg = as.character(mtcars$mpg)
mtcars$cyl = as.character(mtcars$cyl)

## the columns we want to convert to numeric
cols = c("mpg", "cyl")

## command to mutate the cols and apply the function as.numeric to them
librar(dplyr)
mtcars %>% mutate(across(all_of(cols), as.numeric))

CodePudding user response:

Here a solution that does not need additional packages as it uses just "base R" functions:

## create a data example
df <- data.frame(
  id = letters[1:10],
  x = as.character(sample(10, 10)),
  y = as.character(runif(10))
)

## convert columns x and y
cols <- c("x", "y")
df[cols] <- lapply(df[cols], as.numeric)

It works with lapply (list apply) because a data.frame is essentally a list of columns.

As an alternative, we can also use type.convert (as suggested by @TarJae) even without dplyr:

df <- type.convert(df, as.is=TRUE)
  • Related