r - sum(.) on a factor column returns incorrect result -
I'm here in a strange fix. I very am using This code data will bring the workspace: There are two columns, but this does not give correct answer. I thought that if I change the class from But I got the same wrong answer. I realized that the problem Step 1: Resume the data: Step 2: Use a different method to change the It works fine! However, I'm not sure what is wrong with the first two ways code> numerical or Answer: Do Edit: Alternatively 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.
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"))
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]
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]
case and
Factor and
is to change numeric class, so I tried a different way and it works Done:
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>
numerical class from factor :
New.Res [, Case: = Straw (case)] New LS [amount (case) = Party]
factor
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.
case: as .numeric (as.character (case)) .
? Factor Help page suggests more efficient as
as.numeric (level (case)) [case] . In comments H / T @ GC
Comments
Post a Comment