16

I have the following data.frame and I want to perform some calculations on the 2nd column.

> test  
  code age
1  101  15
2  102  25
3  103  16
4  104  u1
5  105  u1
6  106  u2
7  107  27
8  108  27

As you can see, the 2nd column does not include only numbers. I omitted these cases:

> new<-subset(test,code<104 | code>106)
> new
  code age
1  101  15
2  102  25
3  103  16
7  107  27
8  108  27

But when I try to do a calculation in a new column this is what I get:

> new["MY_NEW_COLUMN"] <- NA
> new
  code age MY_NEW_COLUMN
1  101  15            NA
2  102  25            NA
3  103  16            NA
7  107  27            NA
8  108  27            NA
> new$MY_NEW_COLUMN <-new[,2] * 5
Warning message:
In Ops.factor(new[, 2], 5) : * not meaningful for factors   

Why does that happen? Any suggestions?

1 Answer 1

32

new[,2] is a factor, not a numeric vector. Transform it first

new$MY_NEW_COLUMN <-as.numeric(as.character(new[,2])) * 5
3
  • You should remind people to read the R-FAQ where this and many other bits of information are distributed with every copy of R.
    – IRTFM
    Nov 14, 2016 at 5:40
  • why not directly as.numeric(new[,2]) ? i want to know the difference
    – MRTgang
    May 31, 2018 at 6:10
  • 1
    see ?as.numeric(): "If ‘x’ is a ‘factor’, ‘as.numeric’ will return the underlying numeric (integer) representation, which is often meaningless as it may not correspond to the ‘factor’ ‘levels’.".
    – scoa
    May 31, 2018 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.