Assigning shorter vector to a longer column in a matrix - r

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

Related

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

How do I match one vectors index positions to a different vectors index positions?

I have two vectors with different values. I have sorted the second vector and need to rearrange the first vector so that it matches the index positions of the second vector. For example if vector B has values 3, 5, 1, 2 rearranged to 1,2,3,5, I need to sort vector A so that the index positions are the same as the positions of vector B rearranged. I've tried:
>sort(VectorB)
>match(c[VectorA], c[sort(VectorB)]
You are looking for VectorA[order(VectorB)]. To understand this issue, try
sig <- order(VectorB)
VectorB[sig]
VectorA[sig]

Creating Vector in R (multiple conditions)

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])

Counting number of elements for each vector in a dataframe on larger than each element in vector 2 in R

Finding number of elements in one vector that are less than an element in another vector
This post has a very similar question, now b is a dataframe instead of a vecor. How do we do the same comparison if a has different length than each vecor in b?
sapply(b, function(x) sum(a < x))
Not sure I understand your question, but:
If a is a scalar, then this same sapply statement returns the number of elements that are larger than a in each column. If a is a vector, you may be interested in sapply(b, function(x) sum(max(a) < x)), to count the number of elements in each column of b that are greater than all the elements in a.

Convert a one column matrix to n x c matrix

I have a (nxc+n+c) by 1 matrix. And I want to deselect the last n+c rows and convert the rest into a nxc matrix. Below is what I've tried, but it returns a matrix with every element the same in one row. I'm not sure why is this. Could someone help me out please?
tmp=x[1:n*c,]
Membership <- matrix(tmp, nrow=n, ncol=c)
You have a vector x of length n*c + n + c, when you do the extract, you put a comma in your code.
You should do tmp=x[1:(n*c)].
Notice the importance of parenthesis, since if you do tmp=x[1:n*c], it will take the range from 1 to n, multiply it by c - giving a new range and then extract based on this new range.
For example, you want to avoid:
(1:100)[1:5*5]
[1] 5 10 15 20 25
You can also do without messing up your head with indexing:
matrix(head(x, n*c), ncol=c)

Resources