-1

I want to divide all values of a column by 3.

When I do this

x$V2/3

It returns an error ‘/’ not meaningful for factors

And when I do this - as.numeric(x$V2)/3

ex :- 39/3 - 3.3333333 istead of 13.

I am not sure what is wrong. Could you please help.

sdu
  • 25
  • 6
  • 1
    Check what is the class of that column with `class(x$V2)` – Sonny Apr 24 '19 at 19:09
  • 1
    Try `as.numeric(as.character(x$V18))/3` – LocoGris Apr 24 '19 at 19:09
  • Perfect! Thank you so much LocoGris – sdu Apr 24 '19 at 19:11
  • 2
    the first error you're getting indicates `x$V2` is a factor. When you import your data did, please use `stringsAsFactors=FALSE`, this will ensure it's not read in as factors. but you might have some values that are not numeric in the data, so look for those as well and clean as necessary – infominer Apr 24 '19 at 19:12
  • Seems relevant: [How to convert a factor to integer\numeric without loss of information?](https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-integer-numeric-without-loss-of-information#3418192) – markus Apr 24 '19 at 19:23
  • Thank you @markus – sdu Apr 24 '19 at 19:59

1 Answers1

1

A quick answer, although this has been partially solved on in the comments.

As Infominer has suggested, the error your getting is that the column you are trying to divide by 3, is actually a factor. Going by the definition of the manual availible here, a factor is

A factor is a vector object used to specify a discrete classification (grouping) of the components of other vectors of the same length

Basically, if you have a vector c("A", "B", "A", "C"), this can either be a character vector or it can be mapped as a factor giving "A" a value of 1 (as it comes first), "B" a value of 2 and "C" a value of 3. These values assigned can be changed but for this it is not important. Running

factorVector <- c("A", "B", "A", "C")
class(factorVector) #[1] "character"
as.numeric(factorVector) #[1] NA NA NA NA  + 1 warning
factorVector <- factor(factorVector)
class(factorVector) #[1] "factor"
as.numeric(factorVector) #[1] 1 2 1 3

Illustrates this property as well as a key difference between factors and characters in R.

Note how when i try to convert a character vector to numeric, it returns NA and throws an error, while the factor returns a number uniquely for each unique instance in factorVector.

One important thing to note is that when one imports data from a *.txt or *.csv file, this might affect how your data is imported. For example read.table(----) is sensitive to the comma seperation in the file. In Denmark the decimal is marked with a comma (,) while columns are seperated with a semi-colon (;). Most implementations standardizes comma as column seperator and a dot (.) as decimal point, and as such a danish file would

  1. Possibly seperate columns incorrectly, if it indentified , as column seperator
  2. If it did find ; as column seperator but kept . as decimal point, decimal numbers could be converted to strings, as the true decimal marker (,) was not identified to be a decimal.

As such it is important to be aware of how the file is set up, when importing and setting the sep (column seperation) and dec (decimal point symbol) when importing data via read.table or the equivalent. For example read.csv("dataFile.txt", sep = ";", dec = ",") would correctly read a danish file format.

It seems your file contains numbers that may not have the standard . as a decimal marker. Make sure to check that your data is imported correctly prior to performing calculations.

Oliver
  • 8,169
  • 3
  • 15
  • 37