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
- Possibly seperate columns incorrectly, if it indentified
,
as column seperator
- 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.