add the rows based on numeric input

table is a dynamic datatable.
if i change numeric input to 16 it should display the row2 and row5 because row2=5 and row5=11
Capture2

df <- iris[, 1:4]
# the rows where sum of columns = 10.2
df[10.2 == apply(df, 1, sum), ]

i dont want the sum of rows . i want the column sum of any row such that it is equal to the input

I don't understand then. The column sum of any row that is equal to the input will be ...the input.

based on numeric input number i want to display only few rows which is equal to the sum of the input number

OK. I think that's what my code above should accomplish.

no that doesnt work @arthur.t

Seems to work for me. It will be up to you to adapt this to your own data, because I don't have access to your data.

df <- iris[, 1:4]
# the rows where sum of columns = 10.2
df2 <- df[10.2 == apply(df, 1, sum), ]
nrow(df)
#> [1] 150
nrow(df2)
#> [1] 4
print(df2)
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1           5.1         3.5          1.4         0.2
#> 5           5.0         3.6          1.4         0.2
#> 29          5.2         3.4          1.4         0.2
#> 40          5.1         3.4          1.5         0.2

Created on 2021-07-27 by the reprex package (v1.0.0)

if sum=10.2(for sepal length column) i want to display iris row 1 nd 4 as output
and that sum=10.2 is given as input in numeric input widget in shiny

iris row 4 has a sum of 9.4. The rows that have a sum of 10.2 are 1, 5, 29, 40.

df <- iris[1:4, 1:4]
df

#if input is 10 for sepal length then output should display the first two rows (5.1+4.9)
#if input is 9.8 for sepal length then row 1 and row 3 should display(5.1+4.7) input is given using numeric input rshiny

df
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 1.3 0.2
4 4.6 3.1 1.5 0.2

output
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2

this is wat i wanted

library(tidyverse)

find_sums <- function(vec, target) {
  # return data frame of matches for each elements of vec that sum to target
  match_df <- expand_grid(
    tibble(row1 = 1:length(vec), value1 = vec),
    tibble(row2 = 1:length(vec), value2 = vec)
  ) %>%
    filter(value1 + value2 == target,
           row1 <= row2) %>%
    mutate(match_id = 1:n()) 
  return(match_df)
}

reference_matches <- function(df, col_name, target) {
  # find the matches of sums in df column that sum to target and return rows of the df
  match_df <- find_sums(pull(df, col_name), target)
  match_df_long <- match_df %>% pivot_longer(contains("row"), values_to = "row_num")
  left_join(match_df_long %>% select(match_id, row_num),
            df %>% mutate(row_num = 1:n()),
            by = "row_num")
}

reference_matches(iris, "Sepal.Length", 10)
#> # A tibble: 376 x 7
#>    match_id row_num Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
#>       <int>   <int>        <dbl>       <dbl>        <dbl>       <dbl> <fct>     
#>  1        1       1          5.1         3.5          1.4         0.2 setosa    
#>  2        1       2          4.9         3            1.4         0.2 setosa    
#>  3        2       1          5.1         3.5          1.4         0.2 setosa    
#>  4        2      10          4.9         3.1          1.5         0.1 setosa    
#>  5        3       1          5.1         3.5          1.4         0.2 setosa    
#>  6        3      35          4.9         3.1          1.5         0.2 setosa    
#>  7        4       1          5.1         3.5          1.4         0.2 setosa    
#>  8        4      38          4.9         3.6          1.4         0.1 setosa    
#>  9        5       1          5.1         3.5          1.4         0.2 setosa    
#> 10        5      58          4.9         2.4          3.3         1   versicolor
#> # ... with 366 more rows

reference_matches(iris, "Sepal.Length", 9.8)
#> # A tibble: 262 x 7
#>    match_id row_num Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>       <int>   <int>        <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#>  1        1       1          5.1         3.5          1.4         0.2 setosa 
#>  2        1       3          4.7         3.2          1.3         0.2 setosa 
#>  3        2       1          5.1         3.5          1.4         0.2 setosa 
#>  4        2      30          4.7         3.2          1.6         0.2 setosa 
#>  5        3       2          4.9         3            1.4         0.2 setosa 
#>  6        3       2          4.9         3            1.4         0.2 setosa 
#>  7        4       2          4.9         3            1.4         0.2 setosa 
#>  8        4      10          4.9         3.1          1.5         0.1 setosa 
#>  9        5       2          4.9         3            1.4         0.2 setosa 
#> 10        5      35          4.9         3.1          1.5         0.2 setosa 
#> # ... with 252 more rows

Created on 2021-07-27 by the reprex package (v1.0.0)

1 Like

is it possible to give input in a numeric input??

I don't understand your question.

Capture2
i hope u understand now!!
basically it has to return rows which satisfies the numeric input condition i.e if input is 19 it jus has to return 1st row it input is not equl to any row then it has to add any two rows and get the nearest values atleast to the specified input

My solution will work with numeric input supplied as the third argument to reference_matches.

reference_matches(iris, "Sepal.Length", 10)
reference_matches(iris, "Sepal.Length", 9.8)

However it will only find pairs of rows that sum to the total. Finding set of 3, 4, 5, + rows that could sum to a total is a more difficult math problem.

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.