Sliding window using R [closed] - r

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a data frame with daily data in R (148 columns by 6230 rows). I want to find the correlations coefficients using sliding windows with length of 600 (days) with windows displacement of 5 (days) and trying to generate 1220 correlation matrices (approx.). All the examples that I saw used only one information vector. There exist an easy way to find those correlation matrices using sliding window? I'll appreciate any suggestion.

If M is the input matrix then each row of out is one correlation matrix strung out column by column:
library(zoo)
out <- rollapply(M, 600, by = 5, function(x) c(cor(x)), by.column = FALSE)
They could be reshaped into a list of correlation matrices, if need be:
L <- lapply(1:nrow(out), function(i) matrix(out[i, ], ncol(M)))
or as an array:
simplify2array(L)

Related

Number of cells of raster under a condition [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I have a binary raster file and I want to count the number of pixels with value one. How do I do that with R?
Ben Bolker's solution, but then with terra (the replacement for raster)
set.seed(101)
library(terra)
r <- rast(matrix(sample(0:1,size=10000,replace=TRUE),100))
global(r, sum)
# sum
#lyr.1 4984
To count the number of 1s in non-binary rasters, and considering NAs, I would prefer
global(r==1, sum, na.rm=TRUE)
over
global(r, function(x) sum(x==1, na.rm=TRUE))
Because the former also works on very large rasters, as "sum" is a known function that can be computed in chunks.
There may well be a more efficient way to do this, but:
set.seed(101)
library(raster)
r <- raster(matrix(sample(0:1,size=10000,replace=TRUE),100))
cellStats(r,function(x,...) sum(x==1)) ## 4984
Actually, if this is a binary raster, cellStats(r, sum) works just as well (and probably quicker?)

Function for matrix in R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm new (very new) in R. I'm struggling with making a function that's supposed to take a matrix (old_matrix) and return a new matrix (new_matrix), but in new_matrix all values in old_matrix that is a prime should be multiplied by 2 when it appears in new_matrix. So the new matrix should look the same as the old matrix, but where a prime occurs in old, this element should be multiplied by 2.
I'm thinking that I should start out with a for loop, but I'm already struggling with how to make the loop go through all elements of the matrix. I appreciate all the help I can get to get closer to making this function!
The isPrime function in the numbers package could be a big help
# Start by creating an example to work with
old_matrix <- matrix(sample.int(100, 25), 5, 5)
# Create your new matrix and determine which numbers are prime
new_matrix <- old_matrix
primeVals <- numbers::isPrime(old_matrix)
# Index into the matrix using the prime value indicator and multiply by 2
new_matrix[primeVals] <- new_matrix[primeVals]*2

Faster method for aggregating this data in R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a fairly large dataset (6.5 M rows, 8 cols) that I'm summarizing in a time series of aggregate counts of observations by day.
I'm currently summing across the intersection of two vectors that are the axes in my time series matrix. The iterations are taking hours to run, and I'm wondering if I'm overlooking something that might give better performance.
My code:
m<-length(datespace)
sensorlist<-as.vector(unique(sensordata$SOURCE))
n<-length(sensorlist)
y <- matrix(0, nrow=m, ncol=n)
colnames(y) <- sensorlist
for(sensor in 1:n){
for(date in 1:m){
count<-sum(as.vector(sensordata$SOURCE==sensorlist[sensor] & di==datespace[date]))
y[date,sensor] = count
}
}
I know FOR loops are less efficient are an indicator that there's probably a better way in R to get this done.
The crux of this problem seems to be a fast way to create a sparse matrix that fills in the missing summary data with zeros.
Pretty sure this is a simple tally:
library(dplyr)
sensordata %>%
group_by(SOURCE) %>% # or maybe group_by(SOURCE, di)?
tally()

Basic matrix math in R [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I want to perform basic math using two matrices.
As my input I have two 3x3 matrices. I would like to divide every integer in matrixA by every integer in matrixB and the output to be one 3x3 matrix of their products.
What is the R function(s) that can do that for me.
Thanks in advance!
It sounds like you want to divide element by element. In this case, you can simply use the / operator.
### Create two matrices
matA <- matrix(1:9, nrow = 3)
matB <- matA
### Divide element by element
matB / matA
### As Frank pointed out, division by 0 goes to Inf
matA[1, 1] <- 0
matB / matA

ttest on many columns in Matlab/R [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Does anybody know the examples on how to run paired ttest in Matlab/R/SAS or Python/Java on many columns (I have 1139 variables) in all combinations or selected respective columns in a loop.
thank you
MATLAB Solution:
If I understand correctly, you're just looking for a way to feed ttest with two different columns from your input matrix everytime. You can get all possible combinations of column pairs using nchoosek:
pairs = nchoosek(1:size(X, 2), 2);
Now you can iterate over these indices, each time invoking ttest with a different pair:
for idx = transpose(pairs)
h = ttest(X(:, idx(1)), X(:, idx(2)));
%// Do something with the result...
end

Resources