logo
Tags down

shadow

Data.table - Subtract pairs of columns


By : Joel A Silverberg
Date : August 02 2020, 08:00 AM
this will help Not sure if I understand your requirements correctly, as there might be a issue with taking difference without first ensuring that you are taking the correct 2 elements for differencing.
But if you assume that the order is always correct (i.e. difference between every 2 rows), an option is to convert a wide format before taking the difference:
code :
library(data.table)
rrm[order(variable, type), g := rowid(type)]
dcast(rrm, variable + g ~ type, value.var="value")[, d := pku - pk0][]
    variable  g         pk0         pku           d
 1:       d1  1  9.83848047  9.85121517  0.01273470
 2:       d1  2  9.83260865  9.85146830  0.01885965
 3:       d1  3  9.81911924  9.82988272  0.01076348
 4:       d1  4  9.83539816  9.84955729  0.01415913
 5:       d1  5  9.82875994  9.84850104  0.01974110
 6:       d1  6  9.84339190  9.86068047  0.01728857
 7:       d1  7  9.82926417  9.82470032 -0.00456385
 8:       d1  8  9.83447069  9.85030651  0.01583582
 9:       d1  9  9.83186515  9.85001546  0.01815031
10:       d1 10  9.82491390  9.81850133 -0.00641257
11:       d9 11 10.64305012 10.62249295 -0.02055717
12:       d9 12 10.64863033 10.59728566 -0.05134467
13:       d9 13 10.64945397 10.61090461 -0.03854936
14:       d9 14 10.70287971 10.68832242 -0.01455729
15:       d9 15 10.65037572 10.60638526 -0.04399046
16:       d9 16 10.64889921 10.61344703 -0.03545218
17:       d9 17 10.64698026 10.61538660 -0.03159366
18:       d9 18 10.65100799 10.61024106 -0.04076693
19:       d9 19 10.62840893 10.57098697 -0.05742196
20:       d9 20 10.62886659 10.62228710 -0.00657949
21:     mean 21 10.18634967 10.19177697  0.00542730
22:     mean 22 10.18323074 10.18338736  0.00015662
23:     mean 23 10.17690302 10.17663512 -0.00026790
24:     mean 24 10.21680005 10.21574084 -0.00105921
25:     mean 25 10.18142407 10.18245526  0.00103119
26:     mean 26 10.18865703 10.18993432  0.00127729
27:     mean 27 10.18469411 10.18178037 -0.00291374
28:     mean 28 10.18555337 10.18447134 -0.00108203
29:     mean 29 10.17899505 10.17634073 -0.00265432
30:     mean 30 10.17541841 10.17374883 -0.00166958
31:   median 31 10.12543247 10.13716609  0.01173362
32:   median 32 10.12054280 10.13694067  0.01639787
33:   median 33 10.12060491 10.13753156  0.01692665
34:   median 34 10.12023764 10.13806559  0.01782795
35:   median 35 10.12172742 10.13700781  0.01528039
36:   median 36 10.12619102 10.14314559  0.01695457
37:   median 37 10.12053163 10.14318633  0.02265470
38:   median 38 10.12393058 10.13973191  0.01580133
39:   median 39 10.11843975 10.13463409  0.01619434
40:   median 40 10.11573985 10.12658083  0.01084098
41:      var 41  0.13861818  0.15092039  0.01230221
42:      var 42  0.13358830  0.12118053 -0.01240777
43:      var 43  0.14075433  0.13094447 -0.00980986
44:      var 44  0.23091842  0.20692249 -0.02399593
45:      var 45  0.13479935  0.12513323 -0.00966612
46:      var 46  0.13331305  0.12879185 -0.00452120
47:      var 47  0.13504633  0.13580084  0.00075451
48:      var 48  0.13449477  0.11868215 -0.01581262
49:      var 49  0.13353367  0.12953169 -0.00400198
50:      var 50  0.13668670  0.14617279  0.00948609
51:  var_eps 51  0.04579254  0.07386206  0.02806952
52:  var_eps 52  0.03936571  0.04443490  0.00506919
53:  var_eps 53  0.04138806  0.05209369  0.01070563
54:  var_eps 54  0.08396788  0.09311348  0.00914560
55:  var_eps 55  0.03867605  0.04699705  0.00832100
56:  var_eps 56  0.04505027  0.05448253  0.00943226
57:  var_eps 57  0.03982819  0.06146928  0.02164109
58:  var_eps 58  0.04107385  0.04364253  0.00256868
59:  var_eps 59  0.04020068  0.05265849  0.01245781
60:  var_eps 60  0.04064481  0.06567682  0.02503201
61: var_pred 61  0.09282564  0.07705833 -0.01576731
62: var_pred 62  0.09422258  0.07674563 -0.01747695
63: var_pred 63  0.09936627  0.07885078 -0.02051549
64: var_pred 64  0.14695054  0.11380901 -0.03314153
65: var_pred 65  0.09612330  0.07813618 -0.01798712
66: var_pred 66  0.08826278  0.07430932 -0.01395346
67: var_pred 67  0.09521814  0.07433156 -0.02088658
68: var_pred 68  0.09342092  0.07503962 -0.01838130
69: var_pred 69  0.09333300  0.07687320 -0.01645980
70: var_pred 70  0.09604189  0.08049596 -0.01554593
    variable  g         pk0         pku           d


Share : facebook icon twitter icon

How to iterate over pairs of columns in a data table to create a new column in R


By : Qusay AH
Date : March 29 2020, 07:55 AM
Does that help I currently have data of complex numbers in the form: , To extract a column you can either use:
code :
dat$column
dat["column",] # Notice the "
dat[paste(...),]
dat$paste(...)

Subtract pairs of columns based on matching column


By : MrDoctype
Date : March 29 2020, 07:55 AM
wish helps you I'll apologise in advance - I know this has likely been answered elsewhere, but I don't seem to be able to find the answer I need, and can't manage to adapt other code I have found to my needs. , Using aggregate in base:
code :
> aggregate(.~FILE, data= DF[, -2], FUN=diff)
  FILE COUNT
1    A    15
2    B    25
3    C    20
> ddply(DF[,-2], .(FILE), summarize, DIFFERENCE=diff(COUNT))
  FILE DIFFERENCE
1    A         15
2    B         25
3    C         20
> # library(data.table)
> DT <- data.table(DF)
> DT[, diff(COUNT), by=FILE]
   FILE V1
1:    A 15
2:    B 25
3:    C 20
> with(DF, by(COUNT, FILE, diff))
FILE: A
[1] 15
----------------------------------------------------------------------------- 
FILE: B
[1] 25
----------------------------------------------------------------------------- 
FILE: C
[1] 20
> tapply(DF$COUNT, DF$FILE, diff)
 A  B  C 
15 25 20 
> # library(doBy)
> summaryBy(COUNT~FILE, FUN=diff, data=DF)
  FILE COUNT.diff
1    A         15
2    B         25
3    C         20
> aggregate(.~FILE, data= DF[, -2], function(x) (x[1]/x[2])*100)
  FILE    COUNT
1    A 40.00000
2    B 16.66667
3    C 60.00000

Working with pairs of related columns (dplyr, tidyr, data.table)


By : user3054247
Date : March 29 2020, 07:55 AM

How to subtract timestamps from two seperate columns, then enter this data into the table


By : Zuhad Mahmood
Date : March 29 2020, 07:55 AM
may help you . I am attempting to create a sign-in, sign-out program using python and mysql on a raspberry pi. I have so far successfully created a system for registering, signing in, and signing out, but am having trouble with the final component. I am trying to subtract the two separate time-stamps (signin, signout) using TIMESTAMPDIFF, and then entering this value into a separate column in the table. However, the statement I am using is not working. , You need an update query not INSERT
code :
UPDATE attendance 
SET `minutes` = TIMESTAMPDIFF(MINUTE, signin, signout);

Subtract pairs of columns


By : laizf
Date : March 29 2020, 07:55 AM
Hope this helps .SD is also a data.table. You have to use [[ to subset a column (or use [, j, with = FALSE]). So this: function(x) (.SD[[2*(x-1)+1]]-.SD[[2*(x-1)+2]]) will work for you case. And when you use single [, it is actually subsetting a row not a column from .SD (like what happens for a normal data.table).
Related Posts Related Posts :
  • multiple usage of ggplot
  • Calculate the number of common occurrences and values ​for each id in R studio
  • Splitting full address column in multiple columns
  • Vector containing a numerical value not being equal to the actual value
  • Extracting all words and clusters of letters in a string and then making each word a seperate piece of data using gsub()
  • Count Duplicates In Vector In R
  • tmap coming up with blank map with one variable (but values are there)
  • Making indexing a row number in map_map2_chr function
  • find frequency of events in groups dplyr
  • Add number of observations per group in barplot (ggplot2)
  • Using case_when() within mutate_at() to recode rows of selected columns with different types with NA
  • R programming: How to count different values’ frequency among all columns?
  • ggplot2: geom_point is sometimes removing NA values depending on the aesthetic used
  • Trying to map a value for geom_vline, but is not plotting in the correct place on the x axis with ggplot in R
  • R - find overlapping dates per group based on another data frame
  • In R how to plot the tail area of a normal distribution using ggplot?
  • Create a loop to generate a series of dataframes in R
  • Is there an R function to select one variable from each group (group_by()) from the dataframe?
  • Alternative of summarise() function in dplyr
  • How to I get scatter plot to have different colour for each value that I am plotting?
  • R: Avoid using a for-loop to sequentially select values in one column and apply a function using the vector of values in
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org