Calculate how many number of times values of row one are greater or less than the values of second row in dataset (C)

a <- c(1,2,3,4,5)
b <- c(-3, 4, 9, 2)
c <- rbind(a,b)

Hello @neeraj_24,

I adjusted your problem slightly as a and b need the same length. You will see I simply transposed and converted to dataframe. Thereafter, I performed a mutate to create when which is larger than the other. FInally I used group_by and summarise to show me how much it was true for which row.

library(tidyverse)

a <- c(1,2,3,4)
b <- c(-3,4,9,2)
c <- rbind(a,b) %>% t() %>%  as.data.frame()

df <- c %>%  mutate(comparison = ifelse(a > b, "a","b"))

df %>% group_by(comparison) %>% summarise(n())
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 2 x 2
#>   comparison `n()`
#>   <chr>      <int>
#> 1 a              2
#> 2 b              2

Created on 2020-10-12 by the reprex package (v0.3.0)

1 Like
a <- c(1,2,3,4,5)
b <- c(-3,4,9,2,5)
c <- rbind(a,b)
sum(c[1,] != c[2,])
#> [1] 4

Created on 2020-10-12 by the reprex package (v0.3.0.9001)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.