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 have a number of the vector with the numbers.
test <- 0.495
vector <- c(0.5715122, 2.2860487, 5.1436096, 9.1441949)
This vector is the need to take an approximate number to the number 0.495.
Help me.
If I've understood correctly, you want to extract the value from a vector that is closest to your test value.
vector[which.min(abs(vector - test))]
#[1] 0.5715122
If two different values could be closest, you could do this:
vector <- c(0.5715122, 2.2860487, 5.1436096, 9.1441949, 0.4184878)
tol <- sqrt(.Machine$double.eps)
vector[which(abs(vector - test) - min(abs(vector - test)) < tol)]
#[1] 0.5715122 0.4184878
tol is a tolerance accounting for floating point accuracy and usually chosen based on help(".Machine").
Related
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 would like to know if there is library like lpSolve for solving that kind of problems, I do not know if this library allows to include in the constraints the product of the two variables or how I can specify "the product is a maximum".
Defining a projection onto the feasible region allows us to use unconstrained optimization to optimize this:
tot <- 10
proj <- function(x) tot * x / sum(x)
res <- optim(1:2, function(x) -prod(proj(x)))
res$convergence
## [1] 0
-res$value
## [1] 25
proj(res$par)
## [1] 4.999799 5.000201
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 4 years ago.
Improve this question
I want to standardize a data set with mean= 3 and std dev =1/3 ; Is there any command to do it?
Scale is for mean =0 , std dev =1.
Here is a snippet from the documentation of scale (?scale).
Usage
scale(x, center = TRUE, scale = TRUE)
Arguments
x a numeric matrix(like object).
center either a logical value or a vector of length equal to the number of columns of x.
scale either a logical value or a numeric vector of length equal to the
number of columns of x.
The following will do exactly as you wanted.
scale(x, center = 3, scale = 1/3)
As a check, try
(x-3)/(1/3)
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
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.
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