Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8711675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:56:14+00:00 2026-06-13T04:56:14+00:00

I have some hospital data in a dataframe, read in from a csv. I

  • 0

I have some hospital data in a dataframe, read in from a csv. I tried to order the dataframe by a user-defined column col and then by the hospital’s name like so:

col <- 'Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia'
hospitals.sorted <- hospitals[order(hospitals[,col], hospitals$Hospital.Name),]

But I think I’m missing something; it seems to sort col like strings:

> hospitals.sorted
... # so far so good # ...
2749                                                   10.0
2831                                                   10.0
2891                                                   10.0
2837                                                   10.1
2824                                                   10.1
2774                                                   10.1
... # not so good # ...
2856                                                   15.7
2834                                                   15.9
2797                                                   16.0
2835                                                    7.4
2850                                                    7.7
2789                                                    8.1
... # there are some non-numeric values at the very bottom # ...
2806                                                    9.9
2867                                                    9.9
2884                                                    9.9
2808                                          Not Available
2913                                          Not Available
2911                                          Not Available

Just to confirm the column is in fact numeric:

> sapply(hospitals, mode)
Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia 
"numeric" 
Hospital.Name 
"numeric"

I don’t know why Hospital.Name is numeric when it’s clearly not.

Other things I tried to no avail:

  • using as.numeric(hospitals[,col]) inside of order
  • removing the “Not Available” values before sorting

I may be missing something fundamental. Halp!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:56:16+00:00Added an answer on June 13, 2026 at 4:56 am

    In data frames, the individual components must be atomic vectors. You are including both numeric and character data in the variable you mention, and as such R will have read that as a character vector. However, due to the default setting of argument stringsAsFactors that character vector will have been converted to a factor. And hence it will look like the numbers are stored as numerics. Those are just labels however and you are being deceived.

    Likewise, the mode() call is deceiving you too. Consider

    > mode(factor(c(1:10, "a")))
    [1] "numeric"
    

    Yet this is clearly not “numeric” data. Next consider

    > mode(factor(letters))
    [1] "numeric"
    

    This belies the fact that internally R’s factors are stored as numeric variables and that is what mode() is telling you. mode() is the wrong tool for this job.

    To test if a variable is numeric, use is.numeric() instead:

    > is.numeric(factor(c(1:10, "a")))
    [1] FALSE
    > is.numeric(factor(letters))
    [1] FALSE
    

    As to the solution. The “Not Available” needs to be set to NA. You can do this when reading the data in by adding na.strings = "Not Available" to the read.table() (or whatever wrapper you used) call. That should be enough to sort out the character > factor conversion.

    Top tip is to always look at the output of str() applied to your object to check that R has read the data in as you wanted it to. So you should do:

    str(hospitals)
    

    and note the types of variables according to R.

    Regarding the other things you tried:

    1. as.numeric(hospitals[,col]) will produce the numeric vector containing the level ID for each element of the factor. If the factor sorts in a particular order, so will it’s levels representation. To convert a factor (it’s labelled version) to a numeric you need an intermediate step: as.numeric(as.character(hospitals[, col])). This will not solve the actual problem you have here though, because you have character data in the variable and R won’t be able to convert it to numeric. It will convert the "Not Available" to NA, which might have worked had you tried as.numeric(as.character(hospitals[, col])).
    2. By removing the "Not Available", I presume by dropping those rows/elements?, will still leave the remaining observations in a factor. Which for reasons mentioned above won’t work as it will alpha sort on the labels/levels.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have some data in a sybase image type column that I want to use
I have some code, which grabs information from a database, then creates a path
I am getting user rolenames from LDAP, and some of them have spaces in
Have some issues with passing large amount of data (3 MB) from uboot to
I have some table in sqlite data base with 12 rows 5 column of
I have some data loaded as a np.ndarray and need to convert it to
I have some arbitrary pixel data that I want to save as a PNG.
I have a line plot of some event at a hospital that I have
I am trying to extract records from a data.frame using grepl. Here are some
Have some runtime defined schedule for one week, for example: <schedule> <s day=Monday, time=1:00:00></s>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.