r - sum(.) on a factor column returns incorrect result -


I'm here in a strange fix. I very am using data.table for routine tasks, but something that would not be able to convince me. I figured out a way around the problem, but I think it is still important to understand that for me what's wrong.

This code data will bring the workspace:

  library (XML) library (data Dtebl) Thurl & lt; - "http://goo.gl/hOKW3a" table & lt; - HTMLTable (Theral) New L & lt; - Datataybl (Table [[2] [4: 5] [- (1: 2)]) Dpres office (name (new. Ars) & lt; - c ( "Party", "case"))   

There are two columns, party and case . Both of which have the default class of factor However, case should be numeric . Lastly, I just want the amount of party for each party . Then something like this should be done:

  new.Res [, zodiac (case), = by party]   

but this does not give correct answer. I thought that if I change the class from case to factor to numeric , then it will work. So I tried the following:

  new.Res [, case: = as.numeric (case)] new.Res [, sum (case), = by party]   

But I got the same wrong answer. I realized that the problem case and Factor and is to change numeric class, so I tried a different way and it works Done:

Step 1: Resume the data:

  theurl & lt; - "http://goo.gl/hOKW3a" tables & lt; - Read the new HTML Tutorial (Theral). L & lt; - data that tables ([[2]] [4: 5] [- (1: 2)]) Dpres office (name (Nyakres) & lt; - c ( "Party", "case")) < / code>  

Step 2: Use a different method to change the numerical class from factor :

  New.Res [, Case: = Straw (case)] New LS [amount (case) = Party]   

It works fine! However, I'm not sure what is wrong with the first two ways factor

code> numerical or integer is to go through the character . This is because internally, a factor is an integer index (which is class refers to the vector). When you ask R to convert it to numeric , it will only convert the underlying index, try not to change the level label.

Answer: Do case: as .numeric (as.character (case)) .

Edit: Alternatively ? Factor Help page suggests more efficient as as.numeric (level (case)) [case] . In comments H / T @ GC

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -