4

The R script below creates a data frame a123 with three columns. Column a1 has three variables occurring at different places with corresponding a2 and a3 values.

a1 = c("A", "B", "C", "A", "B", "B", "A", "C", "A", "C", "B") a2 = c( 10, 8, 11 , 6 , 4 , 7 , 9 , 1 , 3 , 2, 7) a3 = c( 55, 34, 33, 23, 78, 33, 123, 34, 85, 76, 74) a123 = data.frame(a1, a2, a3) 

My need is that I want a3 column values corresponding to a1 column values to be arranged in ascending order based on the order of a2 values. Also, if common a2 values are encountered, the corresponding a3 column values should be arranged in ascending order. For example, say value "A" in column a1 has following values in a2 and a3,

a2 = c(10, 6, 9, 3) a3 = c(55, 23, 123, 85) 

The values can be like:

a3 = c(123, 23, 85, 55) 

Expected Outcome:

a1 = c("A", "B", "C", "A", "B", "B", "A", "C", "A", "C", "B") a2 = c( 10, 8, 11, 6, 4, 7, 9, 1, 3, 2, 7) a3 = c( 123, 78, 76, 23, 33, 34, 85, 33, 55, 34, 74) a123 = data.frame(a1, a2, a3) 

Thanks and please help. Note: Please try to avoid loops and conditions as they might slow the computation based on large data.

4
  • 1
    Did you mean 85 23 123 55 for your example? It's not quite clear to me what your logic is. Commented Dec 30, 2017 at 19:22
  • Yesterday you have posted the same question: stackoverflow.com/questions/48023937/… ...but honestly you didn't better the effectivness of your request... Commented Dec 30, 2017 at 19:47
  • Take a look at the tapply() and order() functions. Commented Dec 30, 2017 at 19:50
  • 1
    @ScipioneSarlo, That post is way different, there, I just needed a column to be sorted, here the entire data frame has to be sorted. Commented Dec 30, 2017 at 20:04

1 Answer 1

2

A solution using dplyr, sort, and rank. I do not fully understand your logic, but this is probably something you are looking for. Notice that I assume the elements in a3 of group A is 123, 55, 85, 23.

library(dplyr) a123_r <- a123 %>% group_by(a1) %>% mutate(a3 = sort(a3, decreasing = TRUE)[rank(-a2, ties.method = "last")]) %>% ungroup() %>% as.data.frame() a123_r # a1 a2 a3 # 1 A 10 123 # 2 B 8 78 # 3 C 11 76 # 4 A 6 55 # 5 B 4 33 # 6 B 7 34 # 7 A 9 85 # 8 C 1 33 # 9 A 3 23 # 10 C 2 34 # 11 B 7 74 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.