-1

I have searched for answers to my question and can't seem to find any answer. I am trying to sort my data so that I can first sort by year of birth and then by last name. Here is my code:

ResidentsBD_99_2015_clean < ResidentsBD_99_2015_clean[order(ResidentsBD_99_2015_clean[, birthdate_year], 
                                                            ResidentsBD_99_2015_clean[, "surname"], 
                                                            decreasing = FALSE), ]

When I run this code, this is the error message that I recieve:

Error in `[.data.frame`(ResidentsBD_99_2015_clean, , birthdate_year) : 
  undefined columns selected
alistaire
  • 38,696
  • 4
  • 60
  • 94
spaghetti
  • 23
  • 5
  • 1
    Can you add the code of your dataframe? You can get it with the `dput()` function. – Barbara Nov 15 '17 at 15:19
  • The output of the dput() function has private information in it, so I would rather not. Any other suggestions? – spaghetti Nov 15 '17 at 15:24
  • 1
    Just make a fake data – S Rivero Nov 15 '17 at 15:24
  • could you create one with fake data or at least show us how the data frame is structured (like column names and so on)? – Barbara Nov 15 '17 at 15:25
  • Your error message suggests that the data frame ResidentsBD_99_2015_clean does not have a column called birthdate_year. If you refer to your data frame columns using the notation above, you should use quotes. I.e. ResidentsBD_99_2015_clean[,"birthdate_year"] instead of ResidentsBD_99_2015_clean[,birthdate_year]. – Otto Kässi Nov 15 '17 at 15:27
  • Possible duplicate of [How to sort a dataframe by column(s)?](https://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-columns) – S Rivero Nov 15 '17 at 15:31

2 Answers2

0

You might just be stuck with typos in your code. birthdate_year should be quoted. It also looks like you have a typo in the assign-operator (<-).

In a more general sense, I prefer ordering with dplyr.

library(dplyr)
ResidentsBD_99_2015_clean <- arrange(ResidentsBD_99_2015_clean, birthdate_year, surname)
A. Stam
  • 1,887
  • 12
  • 26
0

From what I can see from your code, it might just be the missing - in the assignment and some small syntax problem. Try this:

ResidentsBD_99_2015_clean<- ResidentsBD_99_2015_clean[order(ResidentsBD_99_2015_clean$birthdate_year, ResidentsBD_99_2015_clean$surname),]
Barbara
  • 1,060
  • 2
  • 8
  • 28
  • This won't work unless it's a data.table or `Rbirthdate_year` and `surname` are vectors outside the table, I think. – Frank Nov 15 '17 at 15:31