0

I have a data frame like this (5 rows and 1 column),

       data
row1    5
row2    4
row3    12
row4    6
row5    7

I want to make a comparison between current rows and following rows, as this table display.

compare    YES  NO
row1<row2       0
row1<row3   1   
row1<row4   1   
row1<row5   1   
row2<row3   1   
row2<row4   1   
row2<row5   1   
row3<row4       0
row3<row5       0
row4<row5   1   

Another, I've typed some codes in R, with for loop.

    for (i in 1:nrow(data)){
  if (data[i,] <data[(i+1):5,]){
    print("1")
  } else { 
    print ("0")
  } 
}

However, I get error information.missing value where TRUE/FALSE needed

Can anyone help me to solve this problem? Or, maybe the apply function is better?

Sorry for my poor English, and big thanks for your precious time!

Min Wu
  • 13
  • 3
  • 2
    Please share sample data in a reproducible and copy&paste-able format and also include your expected output; screenshots are never a good idea. For details please review how to provide a [minimal reproducible example/attempt](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Maurits Evers Feb 28 '19 at 03:31
  • Yes and thanks. I edited just now, could you check for me again? – Min Wu Feb 28 '19 at 04:57

1 Answers1

0

I'm not quite clear on what your final goal is; your expected output looks like an awkward data format. I assume that this is to adhere to some form of custom/legacy data formatting requirements.

That aside here, you could use outer to do all pairwise comparisons, and then do some data reshaping

library(tidyverse)
outer(df$data, df$data, FUN = function(x, y) x < y) %>%
    as.data.frame() %>%
    rowid_to_column("rowx") %>%
    gather(rowy, val, -rowx) %>%
    mutate(
        rowx = paste0("row", rowx),
        rowy = sub("V", "row", rowy)) %>%
    filter(rowx < rowy) %>%
    unite(compare, rowx, rowy, sep = "<") %>%
    transmute(
        compare,
        Yes = if_else(val == TRUE, 1, 0),
        No = if_else(val == FALSE, 1, 0))
    )
#     compare Yes No
#1  row1<row2   1  0
#2  row1<row3   1  0
#3  row2<row3   1  0
#4  row1<row4   1  0
#5  row2<row4   0  1
#6  row3<row4   0  1
#7  row1<row5   1  0
#8  row2<row5   1  0
#9  row3<row5   0  1
#10 row4<row5   1  0

Sample data

df <- read.table(text =
    "data
1   0.05493405
2   0.07844055
3   0.12901255
4   0.0655028
5   0.078554925", header = T)
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • Thanks firstly! And apologize for my inaccurate description. This time, I changed my two data frame a little. What my real aim is to compare every row and the following other rows, in the first data. For example, row1 compares with row2, row3, row4, row5, and row2 compares with row3,row4, row5, analogous to row3, row4, row5. As the result, row1(5) – Min Wu Feb 28 '19 at 06:42
  • @MinWu I’m very confused; what you describe is exactly what I’m doing with `outer`. – Maurits Evers Feb 28 '19 at 06:58
  • 1
    It' works! Really thanks! Wish you have a great day!! – Min Wu Feb 28 '19 at 07:19