Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in R Programming by (5.3k points)

Is there an easier way to ensure that a data frame's rows are ordered according to a "target" vector as the one I implemented in the short example below?

df <- data.frame(name = letters[1:4], value = c(rep(TRUE, 2), rep(FALSE, 2)))


#   name value

# 1    a  TRUE

# 2    b  TRUE

# 3    c FALSE

# 4    d FALSE

target <- c("b", "c", "a", "d")

This somehow seems to be a bit too "complicated" to get the job done:

idx <- sapply(target, function(x) {

    which(df$name == x)


df <- df[idx,]

rownames(df) <- NULL


#   name value

# 1    b  TRUE

# 2    c FALSE

# 3    a  TRUE

# 4    d FALSE

1 Answer

0 votes

To order rows of a data frame according to a vector with a specific order, use the match() function in R.

The match() function returns a vector of the position of the first occurrence of the first vector in the second vector. If the element of the first vector does not exist in the second vector, NA is returned.

The basic syntax is given below:

match(x, table, nomatch = NA_integer_, incomparables = NULL)



vector or NULL: the values to be matched.



vector or NULL: the values to be matched against.



the value to be returned in the case when no match is found. Note that it is coerced to integer.



a vector of values that cannot be matched. Any value in x matching value in this vector is assigned the nomatch value. For historical reasons, FALSE is equivalent to NULL.

In your case:

df <- data.frame(name=letters[1:4], value=c(rep(TRUE, 2), rep(FALSE, 2))) target <- c("b", "c", "a", "d") df[match(target, df$name),]


name value 2 b TRUE 3 c FALSE 1 a TRUE 4 d FALSE

Browse Categories