0

Basic data was generated using a SQL query and the intention is to process data in R. However, while importing from a .csv or from .xlsx, R imports numbers as characters in spite of changing the data-type in the built-in import tool. Further, while performing basic arithmetic operations, following errors were encountered: In Ops.factor((data$A), (data$B)) :‘/’ not meaningful for factors

Is there a simple way to solve this?

  • Data-set was analysed using the str() function, which revealed that R imported the particular columns as factors.
  • Used package varhandle and function unfactor to unfactorize the data
  • Used as.numeric for some columns which were read as characters instead of factors
  • Tried changing data-types in Excel before importing

    data$A <- unfactor(data$A)

    data$B <- unfactor(data$B)

    data$PERCENTAGE <- (data$B)/(data$A)*100

By what means can R import the data as per specified data-types?

Thank you for the help in advance!

marine8115
  • 578
  • 3
  • 19

1 Answers1

2

For csv files I would recommend read_csv from Hadley Wickham's excellent Tidyverse package. It has intelligent defaults that cope with most things I throw at it.

For .xlsx, there is read_excel, also from the Tidyverse package (there are other packages available). Or, alternatively just export a .csv from within Excel and use read_csv.

[Note the Tidyverse's will import these files as a "tibble" which is essentially a data frame on steroids without some of the headaches but is easily converted to a data.frame if you prefer.]

indubitably
  • 279
  • 2
  • 7