Categories
Mastering Development

Pick a slice from R array according to coordinates

I have two vectors x and y of coordinates and a 3D array A in R. I want to produce a matrix, where the i’th row is A[x[i], , y[i]]. If A was 2D, I believe I could use A[cbind(x,y)]. For the 3D array, I think the following works, but it’s kind of slow: sapply(1:length(x), […]

Categories
Mastering Development

boot function with purr::map in R

I’m studying about bootstrap two sample t test with boot package. In gene expression matrix, I want to compare genes between conditions and my aim is to find expressed genes. I have a matrix 5*12(5 control, 7 treatment and 5 genes) and firstly I converted this data matrix to tibble format as two long vector […]

Categories
Mastering Development

Connection Algorithm with Constraints

I have block categories A, B, C, … and each category has blocks. all blocks in each category have ports p0, p1, p2, … and have constraints to connect as: A: a0, a1, a2, … p0: B, C p1: B p2: B, C p3: C B: b0, b1, b2, … p0: A C: c0, c1, […]

Categories
Mastering Development

R Rolling values by group

I would like to do some calculations using frollaply() or rollapplyr() with a conditional factor. I have the following data df <- tibble(w = c(NA, NA, "c1", NA, NA, "c2", NA, NA, "c3", NA, NA, "c4"), x = 1:12, y = x * 2) %>% as.data.table() Using data.table I generate the following result. df[, sumx […]

Categories
Mastering Development

C++ inheritance and assignment operator

I am using CRTP to implement a C++ feature. Now I’ve faced a situation like this. template<typename T> struct A { char a; T operator+(const T& other){ T t; t.a = a + other.a; return t; } }; struct B : A<B>{ }; struct C : B { C& operator=(const C& other){ a = other.a; […]

Categories
Database Development

Sum Scores in Tree Structure Based on Rollup Type

I am trying to calculate totals for each node of a tree structure, Calculating the total depends on roll up type I have Structure like below if node type is current list = Look to current node, no need I go down in the tree if node type is child = ignore the current node […]

Categories
Mastering Development

delete column in data.table in R based on condition (row wise)

How can I remove every col that meets a specific condition from a specific row? In my case remove each col with =="-" in the first row. I want to do it completely inside [ ] without copying the data in memory, and if possible avoiding an external function. mtcars[1,c(2,4,6)] <- "-" dt <- data.table(mtcars) […]

Categories
Mastering Development

How do I find the average of a column in a list?

I have a list of vectors currently: -4.5 4.5 -8.66025 -3.5 4.5 -8.66025 -2.5 4.5 -8.66025 -1.5 4.5 -8.66025 -0.5 4.5 -8.66025 I’m generating these earlier in the program and can calculate the size beforehand (or after too). How do I find the average of each column (0, 1, 2)? In Python, I can do: […]

Categories
Linux Mastering Development

Why do BSD and GNU grep give different results here?

I distribute a slightly modified version of a public CSV file. To generate the modified CSV file I use a short one-line grep command: grep -Ev ‘^([A-Za-zA-Za-z]|[0-90-9]*),’ input.csv Actual project is here. A user reported that BSD grep on a Mac gets different results from GNU grep even with correct locale settings. It isn’t clear […]

Categories
Mastering Development

How to change CSV values into multiple limited columns in Postgres?

I have a table with a single column in a Postgres 13.1 database. It consists of many rows with comma-separated values – around 20 elements currently. I want to split the data into multiple columns. But I have only a limited number of columns say 5 and more than 5 CSV values in a single […]