Having issues using order function in R

ⅰ亾dé卋堺 提交于 2019-12-13 06:39:28

问题


My data.frame is stateData and when I execute stateData[order(stateData$"heart failure"),], with heart failure being a column name, I'm getting my dataframe back with the heart failure column having increasing values like this:

10.0, 10.1, 10.3, 10.7, 15.0, 15.1, 15.9, 8.1, 8.3, 8.9, 9.0, 9.1

Here are details:
dput(head(stateData)) heart failure = structure(c(97L, 44L, 25L, 6L, 52L, 57L ), .Label = c("10.0", "7.2", "7.3", "7.4", "7.5", "7.6", "7.7", "7.8", "7.9", "8.0", "8.1", "8.2", "8.3", "8.4", "8.5", "8.6", "8.7", "8.8", "8.9", "9.0", "9.1", "9.2", "9.3", "9.4", "9.5", "9.6", "9.7", "9.8", "9.9", "Not Available"), class = "factor"),

Why is it not sorting it all the way?

Any help is appreciated! Thank you!

Edit: Here is my solution! I got it, thanks for all of the advice!

stateData[,"heart failure"] <- as.numeric(levels(stateData["heart failure"])[stateData[,"heart failure"]]) 
sortedData <- stateData[order(stateData[,"heart failure"]),]

回答1:


The column stateData$"heart failure" is a factor, so when R sorts it, it puts it in alphabetical order. If you want the data sorted numerically, try this:

 stateData$"heart failure" <- as.numeric(levels(stateData$"heart failure"))[stateData$"heart failure"]
 stateData[order(stateData$"heart failure"),]


来源:https://stackoverflow.com/questions/23482638/having-issues-using-order-function-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!