0
structure(list(year = structure(1:24, .Label = c("1990", "1991", 
"1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", 
"2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", 
"2008", "2010", "2011", "2012", "2013", "2009"), class = "factor"), 
    val = c(8, 3, 1, 17, 5, 9, 6, 7, 3, 15, 11, 6, 2, 11, 15, 
    11, 15, 1, 14, 2, 15, 4, 6, 0)), .Names = c("year", "val"
), row.names = c(NA, 24L), class = "data.frame")

And I want to sort the year column.

df<-df[order(year),]

But I get this error:

Error in order(year) : argument 1 is not a vector

How can I fix this?

Edit:

df<-df[order(df$year),]

Output

"1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", 
"2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", 
"2008", "2010", "2011", "2012", "2013", "2009"), class = "factor"), 
    val = c(8, 3, 1, 17, 5, 9, 6, 7, 3, 15, 11, 6, 2, 11, 15, 
    11, 15, 1, 14, 2, 15, 4, 6, 0)), .Names = c("year", "val"
), row.names = c(NA, 24L), class = "data.frame")
maximusdooku
  • 5,242
  • 10
  • 54
  • 94
  • Hi maximusdooku, could you please include a which language this is written in? Additionally, your code is pretty poorly formatted, I would advise that you consider adding line endings and tabs. – csga5000 Jan 19 '16 at 21:01
  • 4
    @csga5000 it's tagged as R and the formatting is like that because it's standard output for R. @maximusdooku, just use `df<-df[order(df$year),]` – romants Jan 19 '16 at 21:02
  • @RomanTsegelskyi oh my mistake. The "r" looked like another block of code, silly me. – csga5000 Jan 19 '16 at 21:05
  • Thanks. But the output doesn't show the sort. It remains unchaged, even though I don't get the error anymore. – maximusdooku Jan 19 '16 at 21:34
  • You can put parentheses around an assignment to print it as well. `(df<-df[order(df$year),])`. Or you can `print(df)`, or just type `df` after modifying it to see the modified version. – Gregor Thomas Jan 19 '16 at 21:43
  • `df %>% arrange(year)` – Brandon Bertelsen Jan 19 '16 at 22:10
  • @RomanTsegelskyi It's not working. I am not getting error anymore. But the sort doesn't happen. – maximusdooku Jan 20 '16 at 02:04
  • @BrandonBertelsen Thanks. But that's not working too. Do I have a datatype problem? – maximusdooku Jan 20 '16 at 02:12
  • 2
    Yes, it looks like it. Convert your `year` column to numeric. `df$year = as.numeric(as.character(df$year))`. – Gregor Thomas Jan 20 '16 at 03:15
  • Interesting that your year variable is a factor. Perhaps when you imported it there was a string in there, maybe a blank character like `" "`? – Brandon Bertelsen Jan 20 '16 at 05:20

0 Answers0