sum(.) on a factor column returns incorrect result

前端 未结 1 802
青春惊慌失措
青春惊慌失措 2021-01-06 08:41

I am in a strange fix here. I am using data.table for a very routine task, but there is something that I am not able to explain. I have figured out a way around

相关标签:
1条回答
  • 2021-01-06 09:44

    The correct way to convert from factor to numeric or integer is to go through character. This is because internally, a factor is an integer index (that refers to a levels vector). When you tell R to convert it to numeric it will simply convert the underlying index, not try to convert the level label.

    Short answer: do Cases:=as.numeric(as.character(Cases)).

    Edit: Alternatively the ?factor help page suggests as.numeric(levels(Cases))[Cases] as more efficient. h/t @Gsee in the comments.

    0 讨论(0)
提交回复
热议问题