Creating Vector in R (multiple conditions) - r

Need to create and print a vector in R that includes the following in this order:
A sequence of integers from 6 to 10 (inclusive)
A twofold repetition of the vector c(2, -5.1, -33)
The value of the sum of 7/42 and 2
a) Then extract the first and last elements of the vector to form another vector
b) Form a third vector from the elements not extracted above
* Use the vectors from (a) and (b) to reconstruct and print the original first vector

That should do it:
a.vec<-c(seq(6,10,1),rep(c(2,-5.1,-33),times=2),(7/42+2))
b.vec<-a.vec[c(1,length(a.vec))]
c.vec<-a.vec[-c(1,length(a.vec))]
a.vec<-c(b.vec[1],c.vec,b.vec[2])

Related

Extract two different vectors from set in R

How can I extract the set of (a) and set of (b) as shown in the image to two different vectors, the first vector for (a) values and the second for (b) values
Using names
> tmp=c("a"=1,"b"=2,"a"=3,"b"=5)
> tmp[names(tmp)=="a"]
a a
1 3

Assign group to elements of a vector based on a second vector in R

Let's assume I have a vector test.
test<-c(7,4,6,8,9,7,10,2,11,15)
And a second vector test_split.
test_split<-c(2,4,3,1)
I would to create a dataframe, where the first column is test and the second column is
(1,1,2,2,2,2,3,3,3,4)
As you can see this vector is made from test_split:
I repeat 2 (= test_split[1]) times 1, then 4 (= test_split[2]) times 2 etc.
Is there an efficient way to do that ?
Best,
W

Matrix multiplication inside for loop in Scilab

I want to multiply each column of matrix A by matrix C. For this I am using for loop as follows:
A=[ 0. 1. 2. 3;0. 1. 2. 3.]
C=[2 0;0 2].
for i=1:4
B(i)=C*A(:,i);
end
But no matrix B(i) is displaying.
The result of C*A(:,i) is a column matrix. To store all columns in a single matrix, you have to use the same notation you used to retrieve a single column from A. Therefore, you should write this in your loop:
B(:,i) = C * A(:,i);

Iterating a vector over a list in R

I am dealing with some computational feature extracting problem from RNA data, and I found myself unable to deal with this question:
I have n sequences (say two for example) from which I obtained an iterated statistic i times (kind of doing a Monte Carlo iteration for analizing distribution of obtained statistics compared with original).
Example:
Say we iterate 10 times
n <- 10
I got a vector of 20 values with all the iterations, but this vector corresponds to two different sequences, so I must divide this vector in two equal parts (the iterations are ordered 1:10 - 1:10 for each sequence).
MFEit <- c(10, 12, 34, 32, 12 .....) ## vector of length 20
MFEit.split <- split(MFEit, ceiling(MFEit.along/n5))
This generates a list of two items each with 10 values, named $1 and $2
On the other hand I have a vector of two values which are the original statistics, each corresponding to each original sequence
MFE <- c(25, 15)
What I want to do is to know how many values of first item in the list MFEit.split, are equal or less than the first value of MFE, and, iteratively, how many values of second item in the list MFEit.split, are equal or less than the second value of MFE, and so on, provided that I would have more than two values or items.
I know how to do it one by one, say:
R <- length(subset(MFEit.split$`1`, MFEit.split$`1`<=MFE[1]))
R <- length(subset(MFEit.split$`2`, MFEit.split$`1`<=MFE[2]))
But... how to include this into a loop so that I can get iteratively each comparison, no matter how many MFE values or items in the list I have?
The desired output would be a vector called R, with n values corresponding to each comparison.
Any help?...

Assigning shorter vector to a longer column in a matrix

I have a matrix:
a<-matrix(NA,ncol=10,nrow=10)
and a vector:
b<-sample(1:100,3)
I would like to fill the first column on the matrix with the vector b.
The length of the column is 10 but the vector is only of length 3.
IS there a way to fill the column with the vector and leave the remaining slots empty?
If you mean by fill that the vector b gets recycled when it runs out of elements, you can use the following:
a[,1] <- rep(b,ceiling(ncol(a)/length(b)))[1:nrow(a)]
In case you just want to modify the number of row entries given by the length of b use
a[1:length(b),1] <- b

Resources