Function for matrix in R [closed] - r

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

Related

How to do elements wise addition or multiplication R with 4 X 3 and 3 X 5 vector 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 2 years ago.
Improve this question
I'm trying to do element wise multiplication of two matrix a and b I'm getting the error saying
Error in a + b : non-conformable arrays
data <- matrix(151:162, nrow=4)
data2 <- matrix(221:235, nrow=3)
Error in a * b : non-conformable arrays
However when I'm doing actual matrix multiplication I'm getting the desired output. Can anyone suggest me how to fix that.
150:162 are 13 elements and 220:235 are 16. length(150:162) will show. These aren't 4x3 or 3x5 matrices.
Update: For elementwise operation the dimensions of the matrices need to be identical. See size(data). So these operations are not possible with 3x4 and 4x3.

How can I create a double loop and populate it with numbers? [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 3 years ago.
Improve this question
I created a 4✕5 matrix and use a double loop to populate it with numbers so that the value of an element in the matrix is its row index raised to the power of its column index (e.g. the value of the element in row 2 and column 3 is 2 raised to the power of 3, i.e. 8).
How can I create a double loop and populate it with numbers?
Just use outer.
outer(1:4, 1:5, "^")
This is just a common way of writing it. I'd appreciate if you upvote/accept answer if it solves your problem.
M = matrix(nrow = 4, ncol = 5)
for (i in seq(nrow(M))){
for (j in seq(ncol(M))){
M[i,j] = i^j
}
}

How to construct matrices/vectors from a table in 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 6 years ago.
Improve this question
I'm quite new to R, and if I imported a .csv file and if rows represent
time and columns represent n variables of interest, how could I construct a
function that returns any given 1xn vector from the table?
P.S. I'm not just interested in constructing a vector, but I will perform
matrix algebra with iterative calculations to estimate parameters, which means
I will need to use a for-loop.
If the data structure contains e.g. m rows and n columns i.e. n variables, you can easily construct the n vectors without much effort.
data<-read.csv(".../file.csv")
class(data)
[1] "data.frame"
class(as.numeric(data[1,]))
[1] "numeric"
So it is not a big deal to convert 1*n matrix i.e. vector of length(ncol(data)).
In a loop just use
data["required Row Number",]
to access the particular row. Each case it will ultimately give 1*n matrix or a vector of length(n)
You can use the function melt() from the package reshape2
Or if you want to use the for loop, try something like:
one_col <- data[,1]
for (i in 2:ncol(data)){
one_col <- rbind(one_col, data[,i])
}

How R multiples vectors that are not compatible for multiplication [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 7 years ago.
Improve this question
When I submit this code to R:
x <- c(1,2,4)
z <- c(7,6,3)
a <- x * z
I get:
a
[1] 7 12 12
So R just multiples element by element. But the two vectors are not compatible for multiplication because the first one has three columns and the second one does not have three rows.
What is happening internally?
Please note that these are vectors; not tables.
This means they can of course be multiplied with each other and would give the expected result through their inner product.

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