How to combine and summarize R data.table rows values from different tables of different sizes?

DanJ

I have a table of (x,y) points and would like to create a second table that summarizes those points.

I would like each row in the summary table to show the sum of all the y's where x is greater than a sequence of thresholds. But I'm having trouble figuring out how to join the threshold value of the row into the inner sum.

I've gotten this far:

samples <- data.table(x=seq(1,100,1), y=seq(1,100,1))
thresholds = seq(10,100,10)
thresholdedSums <- data.table(xThreshold=thresholds, ySumWhereXGreaterThanThreshold=sum(samples[x > xThreshold, y]))

Error in eval(expr, envir, enclos) : object 'xThreshold' not found

How would I accomplish this, or is there a different way to do this sort of thing?

To clarify desired output:

thresholdedSums = 
[
  (row 1) threshold = 10, ySumWhereXGreaterThanThreshold = sum of all y values in samples[] where x > 10,
  (row 2) threshold = 20, ySumWhereXGreaterThanThreshold = sum of all y values in samples[] where x > 20,
  ... etc ...
]
Phann

The result can be given by the following code. This solution is not completely based on data.table but works robustly.

thresholdedSums <- data.table(
                     thres = thresholds,
                     Sum = sapply(thresholds, function(thres) samples[x > thres, sum(y)])
                   )

#    thres  Sum
# 1:    10 4995
# 2:    20 4840
# 3:    30 4585
# 4:    40 4230
# 5:    50 3775
# 6:    60 3220
# 7:    70 2565
# 8:    80 1810
# 9:    90  955
# 10:   100   0

Additional explanation: sapply(thresholds, function(thres) samples[x > thres, sum(y)]) returns a vector of the same length as thresholds. You can read it as: For every element in thresholds execute the function function(thres) samples[x > thres, sum(y)] and return the result as a vector. In comparison to a for-loop this procedure is normally better in performance and easier to read.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Combine rows from two tables with different columns?

From Dev

Summarize different tables into one table

From Dev

Combine tables with different data

From Dev

Combine field values from different rows

From Dev

Combine data from two different junction tables

From Dev

Combine data from two different junction tables

From Dev

Kusto: Summarize different rows having real number values in a column in fixed bins of fixed sizes

From Dev

combine different row's values in table in r

From Dev

SQL Server - How to SELECT values from different rows but in the same table

From Dev

How to combine different columns of characters from the same data frame in R

From Dev

How to get data from different tables into a singe table

From Dev

Pandas: combine data frames of different sizes

From Dev

How to connect rows from different tables

From Dev

Pandas sum values in rows of different sizes

From Dev

How to combine two lists of different sizes

From Dev

How to combine two lists of different sizes

From Dev

How do I get values from a nested list in R when the sublists are different sizes and not named?

From Dev

R extracting rows with different values from matrices

From Dev

PHP collect and combine different data from two tables

From Dev

Combine two data frames with different number of rows in R

From Dev

Python combine rows from different files into one data file

From Dev

How to get different values from different tables in Treeview / Tkinter (SQLite)?

From Dev

How to combine graph of different rows (data frame) & plot in a same graph in R?

From Dev

Combining tables from different numbers of rows with a master MAP table

From Dev

How to fetch data from two different tables

From Dev

how to summarize grouped data with different functions that return different numbers of return values?

From Dev

Sum data from two tables with different number of rows

From Dev

Sum data from two tables with different number of rows

From Dev

Calculate difference of variable values from different rows based on combination of other variables' values from different rows in R

Related Related

  1. 1

    Combine rows from two tables with different columns?

  2. 2

    Summarize different tables into one table

  3. 3

    Combine tables with different data

  4. 4

    Combine field values from different rows

  5. 5

    Combine data from two different junction tables

  6. 6

    Combine data from two different junction tables

  7. 7

    Kusto: Summarize different rows having real number values in a column in fixed bins of fixed sizes

  8. 8

    combine different row's values in table in r

  9. 9

    SQL Server - How to SELECT values from different rows but in the same table

  10. 10

    How to combine different columns of characters from the same data frame in R

  11. 11

    How to get data from different tables into a singe table

  12. 12

    Pandas: combine data frames of different sizes

  13. 13

    How to connect rows from different tables

  14. 14

    Pandas sum values in rows of different sizes

  15. 15

    How to combine two lists of different sizes

  16. 16

    How to combine two lists of different sizes

  17. 17

    How do I get values from a nested list in R when the sublists are different sizes and not named?

  18. 18

    R extracting rows with different values from matrices

  19. 19

    PHP collect and combine different data from two tables

  20. 20

    Combine two data frames with different number of rows in R

  21. 21

    Python combine rows from different files into one data file

  22. 22

    How to get different values from different tables in Treeview / Tkinter (SQLite)?

  23. 23

    How to combine graph of different rows (data frame) & plot in a same graph in R?

  24. 24

    Combining tables from different numbers of rows with a master MAP table

  25. 25

    How to fetch data from two different tables

  26. 26

    how to summarize grouped data with different functions that return different numbers of return values?

  27. 27

    Sum data from two tables with different number of rows

  28. 28

    Sum data from two tables with different number of rows

  29. 29

    Calculate difference of variable values from different rows based on combination of other variables' values from different rows in R

HotTag

Archive