[Solved] Naming columns in a data table in R

I don’t word with data tables, but this is a solution that would work for data frames, and should hopefully generalize. The strategy is to use the fact that you can fill one vector with another vector, without ever having to use a loop. # make the example data sets D1 <- as.data.frame(matrix(data=(1:(20*181)), nrow=20, ncol=181)) … Read more

[Solved] Matching Data Tables by five columns to change a value in another column

In R it is always preferable to avoid loops wherever possible, as they are usually much slower than alternative vectorized solutions. This operation can be done with a data.table join. Basically, when you run dt1[dt2]; you are performing a right-join between the two data.tables. The preset key columns of dt1 determine which columns to join … Read more

[Solved] How adjust code functionality to specifications using data.table function

With data.table, we can specify the .SDcols to select the ‘DR’ columns or ‘date_cols’ and assign back the output to those, then instead of using rowwise matching, use a row/column indexing to extract the values to create the ‘Result’ library(data.table) # get the column names that starts with DR dr_names <- grep(“^DR”, names(df1), value = … Read more

[Solved] I want to summarize by a column and then have it take the sum of 1 column and the mean of another column

The crucial point in OP’s approach is the staggered aggregation (see the related question row not consolidating duplicates in R when using multiple months in Date Filter). The OP wants to aggregate data across a number of files which apparently are too large to be loaded altogether and combined into a large data.table. Instead, each … Read more