user91551
user91551

Reputation:

Taking Log of data in R

So I have the following data which gives this output when typed into R.

Value
#  [1] 8,540.40  9,573.90  8,856.40  8,841.30  8,761.50  9,811.50  8,667.20  8,799.90 
#  [9] 8,714.90  9,820.70  8,787.40  8,675.90  8,661.60  9,945.70  9,027.20  9,086.90 
# [17] 9,280.70  10,639.10 9,640.20  9,527.40  9,728.20  11,193.20 10,077.50 10,197.00
# [25] 10,281.40 11,871.40 10,870.20 11,035.30 11,174.60 12,671.40 11,453.20 11,361.40
# [33] 11,597.90 13,387.10 12,468.10 12,373.00 12,802.50 14,565.70 13,476.70 13,228.10
# [41] 13,681.00 15,180.40 14,290.10 14,021.40 14,305.80 16,348.60 15,468.10 15,080.50
# [49] 15,067.10 17,000.80 15,828.90 15,072.70 15,204.00 16,726.20 14,975.20 14,859.70
# [57] 15,208.60 17,194.10 15,834.50 15,599.50 15,722.60 17,444.40 16,322.90 16,180.60
# [65] 16,499.40 18,637.30 17,171.40 16,947.60 16,842.20 19,149.80 17,726.10 17,493.90
# [73] 17,600.00 19,906.00 18,321.40 18,160.00 18,319.10 20,850.90 19,253.40 18,908.60
# 80 Levels: 10,077.50 10,197.00 10,281.40 10,639.10 10,870.20 11,035.30 ... 9,945.70

And I want to take the log of my data so I thought all I needed to was to use

log(Value)

However when I do this I get the following error message?

Error in Math.factor(c(61L, 75L, 70L, 69L, 66L, 78L, 63L, 68L, 65L, 79L,  : 
  ‘log’ not meaningful for factors

Can someone please explain to me why my data is not able to have the log taken of it?

Upvotes: 1

Views: 5465

Answers (2)

Tim
Tim

Reputation: 7474

Your data object is of factor data type, that means that the "numbers" you see are just labels of some categories.

Let's re-create your data:

x <- c("8,540.40", "9,573.90","8,856.40", "8,841.30", "8,761.50", "9,811.50", "8,667.20", "8,799.90")
x <- as.factor(x)

calling x object shows you just that

> x
[1] 8,540.40 9,573.90 8,856.40 8,841.30 8,761.50 9,811.50 8,667.20 8,799.90
Levels: 8,540.40 8,667.20 8,761.50 8,799.90 8,841.30 8,856.40 9,573.90 9,811.50

you can also check the objects class to be sure

> class(x)
[1] "factor"

so to make any numerical operations you have to transform the object into numeric values. If you do it by simply transforming using as.numeric function, what you will get is just the category numbers

> x
[1] 8,540.40 9,573.90 8,856.40 8,841.30 8,761.50 9,811.50 8,667.20 8,799.90
Levels: 8,540.40 8,667.20 8,761.50 8,799.90 8,841.30 8,856.40 9,573.90 9,811.50
> as.numeric(x)
[1] 1 7 6 5 3 8 2 4

This result is clearly wrong. What you need to do is to first translate the etiquettes into number-like format and then, transform them to numeric data. You should start with removing the commas in your data

> sub(",", "", as.character(x))
[1] "8540.40" "9573.90" "8856.40" "8841.30" "8761.50" "9811.50" "8667.20" "8799.90"

and then transforming this character stings to numeric values

> as.numeric(sub(",", "", as.character(x)))
[1] 8540.4 9573.9 8856.4 8841.3 8761.5 9811.5 8667.2 8799.9

with such values you can do all the kinds of numerical operations, including taking logs. Here function as.character has transformed factor etiquettes into character strings, then sub was used to remove commas, and its output was transformed into numeric data type.


In the future you should think of setting the stringsAsFactors parameter to FALSE (see, e.g. here). R by default reads non-numbers as factors, what can be misleading since such data may appear as numbers, but has no numerical meaning. With stringsAsFactors = FALSE what you get in the worst case are character strings instead of factors, so every time you try to make any numerical operations, you see error message and strings are less prone to confuse them with numbers. Your data representation was also ambiguous (it has both commas and dots), so the decimal sign was not clear for the software. See also here for similar problem.

Upvotes: 12

metjush
metjush

Reputation: 111

As Hatshepsut writes, your data is not stored as numbers, but as factors/text because of formatting - the thousands separator prevents R from automatically recognizing it as numbers.

Upvotes: 0

Related Questions