0

I have two columns col32_Dokh and col32_Pesa in a dataframe which contains "00", "01", ... Characters. I want to convert them to integer.

    df_integer <- transform(df_char, col32_Dokh = as.integer(col32_Dokh),
                                     col32_Pesa = as.integer(col32_Pesa))

but the conversion fails in this way:

RStudio Result What's happening here? What should I you do to solve it?

Amin
  • 57
  • 1
  • 8
  • make your table without the conditional stuff – user12912834 Sep 13 '15 at 02:53
  • I an amateur R user, I didn't get what you mean. Please explain more. @user12912834 – Amin Sep 13 '15 at 02:56
  • examine what `df_integer$col32_Dokh == 0` is by itself. You will see it is a a logical vector. So, when you tabulate a logical vector, you will have counts of TRUE and FALSE. What you want is the table without the conditional part (`== 0`) – user12912834 Sep 13 '15 at 02:59
  • Got it! I'm neither good at English! @user12912834 – Amin Sep 13 '15 at 03:00
  • Your original data is probably of class `factor`, which when converted to numeric is transformed into the factor number. Check the output of these: `a <- factor("02"); as.integer(a)` and `as.integer(as.character(a))`. If that's the case then just use `as.character` before `as.integer`, or, even better, make sure your data isn't loaded as factor. – Molx Sep 13 '15 at 04:01
  • You are right. See the post I added. There's even better way to solve the problem. @Molx – Amin Sep 13 '15 at 04:20

1 Answers1

0

Thanks to this post, I found the answer:

col32_Pesa = as.numeric(levels(col32_Pesa))[col32_Pesa]
Community
  • 1
  • 1
Amin
  • 57
  • 1
  • 8