sorting - breaking ties in dataframe (dplyr, data.table, base r) -


thanks response far.

i've spent few more hours on problem , think it's best reframe question. no longer think dplyr work. here issue.

constrain: require access column name programmatically (use of dplyr creates problems).

preferred, not essential: solution without dataframe copy.

code set up:

set.seed(11) n <- 12 <- sample(letters, n, replace=false) b <- c( rep(c("aa"), 4), rep(c("ba"), 4),rep(c("ca"), 4)) c <- sample(4:10, n, replace=true)  df <- as.data.frame(cbind(a,b,c)) dt <- as.data.table(df)  rank_tb <- dt[order(b,c,a)] 

output:

    b  c 1:  e aa 4 2:  m aa 5 3:  b aa 6 4:  o aa 7 5:  ba 5 6:  d ba 6 7:  p ba 7 8:  u ba 9 9:  q ca 5 10: v ca 5 11: j ca 8 12: x ca 9 

rank_tb gets me half way there, note grouping on column "b" preserved, dataframe sorted on column "c" group , ties broken column "a" -> see row 9 , 10. like, in end, following addition:

    b  c rank 1:  e aa 4 1 2:  m aa 5 2 3:  b aa 6 3 4:  o aa 7 4 5:  ba 5 1 6:  d ba 6 2 7:  p ba 7 3 8:  u ba 9 4 9:  q ca 5 1 10: v ca 5 2 11: j ca 8 3 12: x ca 9 4 

which ranked column on "c" grouped column "b" ties broken "a".

below text of original question, again time..

is there method of using second column break ties of sort functions row_number in dplyr package? @ present have:

dat <- data %>%  filter(!is.na(col1)) %>% group_by(col2) %>% filter(row_number(col1) == 1) 

at present unsure how row_number breaks ties in col1, , specify separate col use break ties:

dat <- data %>%  filter(!is.na(col1)) %>% group_by(col2) %>% filter(row_number(col1, col3) == 1) 

thanks in adavance

i not quite sure if understand question. if want first row of each group, can use

data %>%  group_by(col2) %>% filter(row_number()==1) 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -