问题
Not sure why this is happening. I have a dataframe df2
with the variables below:
EVTYPE TOTAL_FATALITIES TOTAL_INJURIES
(fctr) (dbl) (dbl)
1 TORNADO 5633 91346
2 EXCESSIVE HEAT 1903 6525
3 FLASH FLOOD 978 1777
4 HEAT 937 2100
5 LIGHTNING 816 5230
6 TSTM WIND 504 6957
> df2$TOTAL_FATALITIES
[1] 5633 1903 978 937 816 504 470 368 248 224 206 204 172 160 133 127 103 101 101
> df2$EVTYPE
[1] TORNADO EXCESSIVE HEAT FLASH FLOOD HEAT LIGHTNING
[6] TSTM WIND FLOOD RIP CURRENT HIGH WIND AVALANCHE
[11] WINTER STORM RIP CURRENTS HEAT WAVE EXTREME COLD THUNDERSTORM WIND
[16] HEAVY SNOW STRONG WIND BLIZZARD HIGH SURF
985 Levels: HIGH SURF ADVISORY COASTAL FLOOD FLASH FLOOD LIGHTNING ... WND
> df2$TOTAL_INJURIES
[1] 91346 6525 1777 2100 5230 6957 6789 232 1137 170 1321 297 309 231 1488 1021
[17] 280 805 152
I am trying to create new column called SevType
-- where I will store whether a value is either an injury or fatal.
However, when I use cbind on df2$EVTYPE
, it converts the factor into a numeric as seen below.
> head(cbind(Event=df2$EVTYPE,Total = df2$TOTAL_INJURIES,Severity="INJURE"))
Event Total Severity
[1,] "834" "91346" "INJURE"
[2,] "130" "6525" "INJURE"
[3,] "153" "1777" "INJURE"
[4,] "275" "2100" "INJURE"
[5,] "464" "5230" "INJURE"
[6,] "856" "6957" "INJURE"
Notice that Event
at [1,] has changed from TORNADO to 834.
Any hints on why this is happening?
回答1:
We are cbind
ing vectors and the output will be a matrix
. The matrix
can hold only a single class
. So, if there is any vector that is non-numeric, it will convert the whole matrix to 'character' and as the first column is already a factor
, we get the numeric levels of that factor. Better would be to use data.frame
data.frame(Event=df2$EVTYPE,Total = df2$TOTAL_INJURIES,Severity="INJURE")
Or we can use bind_cols
or data_frame
from dplyr
来源:https://stackoverflow.com/questions/33872034/cbind-converting-factor-to-numeric