user183239
user183239

Reputation: 123

Order function in R: argument lengths differ

I'm getting the following error in R:

argument lengths differ.

I have a data set I would like to order on two columns, first on caseID, then on a column that contains a timestamp. I use the following code:

mydata <- mydata[order(mydata[ ,col1], mydata[ ,col2], decreasing = FALSE),]

Col1 and col2 are two variables holding an integer. I have looked at similar questions and tried the solutions that were proposed there, but nothing worked ;).

Could someone please help me?

Kind regards

Upvotes: 10

Views: 16162

Answers (3)

maria118code
maria118code

Reputation: 163

Probably it's nice to check this similar post out, uses dplyr package to solve it and it helped me: Arrange within a group with dplyr

This might do the trick:

library(dplyr)
mydata <- mydata %>%
  arrange(
    col1,
    col2,
    desc(col3)
  )

Upvotes: 1

Cleve Davis
Cleve Davis

Reputation: 45

I was having this same problem, but was able to get my code working. Try this code.

with(mydata, mydata[order(col1,col2),]). 

The result is decreasing, so adding function decreasing = False was not necessary. Hope that helps.

Upvotes: 2

Greg Snow
Greg Snow

Reputation: 49650

R thinks that you 2 columns have different lengths, sometimes that happens when you accidentally access a column that does not exist, check the values of col1 and col2 to make sure that they are appropriate numbers. Also look at length(mydata[,col1]) and length(mydata[,col2]) to see if those 2 values match. Also check for missing , or other punctuation, sometimes if you don't have the syntax exactly right then you get a list of length 1, or a single element vector which does not match the other vector in length.

Upvotes: 9

Related Questions