create new vector from existing vectors by using "rep" - r

Suppose I have the following two vectors,
a<-c(2,3,5)
b<-c(1,3,2)
Now I want to create a new vector c with this results from a and b,
2, 3, 3, 3, 5, 5
I tried this code, but it just does not work, I am stocked here. Help please. How can I get the results showed above?
for (i in 1:3){
c<-rep(a[i], each=b[i])
}

rep(a,b) is what you're looking for.

Related

Is there a native R syntax to extract rows of an array?

Imagine I have an array in R with N dimensions (a matrix would be an array with 2 dimensions) and I want to select the rows from 1 to n of my array. I was wondering if there was a syntax to do this in R without knowing the number of dimensions.
Indeed, I can do
x = matrix(0, nrow = 10, ncol = 2)
x[1:5, ] # to take the 5 first rows of a matrix
x = array(0, dim = c(10, 2, 3))
x[1:5, , ] # to take the 5 first rows of a 3D array
So far I haven't found a way to use this kind of writing to extract rows of an array without knowing its number of dimensions (obviously if I knew the number of dimensions I would just have to put as many commas as needed). The following snippet works but does not seem to be the most native way to do it:
x = array(0, dim = c(10, 2, 3, 4)
apply(x, 2:length(dim(x)), function(y) y[1:5])
Is there a more R way to achieve this?
Your apply solution is the best, actually.
apply(x, 2:length(dim(x)), `[`, 1:5)
or even better as #RuiBarradas pointed out (please vote his comment too!):
apply(x, -1, `[`, 1:5)
Coming from Lisp, I can say, that R is very lispy.
And the apply solution is a very lispy solution.
And therefore it is very R-ish (a solution following the functional programming paradigm).
Function slice.index() is easily overlooked (as I know to my cost! see magic::arow()) but can be useful in this case:
x <- array(runif(60), dim = c(10, 2, 3))
array(x[slice.index(x,1) %in% 1:5],c(5,dim(x)[-1]))
HTH, Robin

How to combine different variables

I am trying to combine two different variables using R. I might be searching for the wrong thing, but I cant find an answer to this question and I am an R novice. Basically I am trying to turn this:
Variable X {1, 3, 5},
Variable Y {2, 4, 6}
Into this:
Variable New{1,3,5,2,4,6}.
The data is a .svs file.
I am having a lot of difficulty finding out how to do this. Any help or links to previous threads would be very helpful.
Try this
x <- c(1,3,5)
y <- c(2,4,6)
c(x,y) # this will make a vector out of x and y

Adding data frames to a list using looping function in R

My data contains of vectors named xDry_i_k and xEthanol_i_k where x can be (A, B, or C), i is pressure (0, 1, 2, 3, or 4 GPa), and k is annealing time (0 for now).
So i have tons of vectors named this way, and would like to gather them in a datalist with frames (2 columns) containing for instance ADry_1_0 and AEthanol_1_0 as these two are related and will be used together in future calculations.
I am new to the for-loops, but i tried searching. So far the best i've been able to come up with is:
datalist=list()
for(x in paste(LETTERS[1:3])){
for(i in seq(4,0,-1)){
for(j in seq(1,length(seq(4,0,-1))*length(paste(LETTERS[1:3])),1)){
datalist[[j]]=data.frame(xDry_i_0,xEthanol_i_0)
}
}
}
I know this code probably seems stupid to the experienced one's of you, but it really bothers me why it isn't this simple....!
For now i get the error "Error in data.frame(xDry_i_0, xEthanol_i_0) : object 'xDry_i_0' not found"
Thanks in advance.

How to get a concrete isomorphism (renaming) in the igraph R package while searching for isomorphic subgraphs?

I have two undirected graphs.
require (igraph)
gsmall <- graph(c(1,3,5,8,3,5), directed = F)
gbig <- graph(c(3, 5, 3, 10, 4, 5, 4, 10, 5, 7, 5, 8, 5, 9, 7, 10, 8, 10, 9, 10), directed = F)
Now I want to know if gbig contains a subgraph which is isomorphic to gsmall. Or to put it precise I want one specific mapping (if it exists).
In the igraph R-package this can be done with the subgraph_isomorphisms function. The problem is that this function gives me all isomorphisms which is expensive already in this small example.
So I tried graph.subisomorphic.lad(gsmall, gbig, all.maps =F) which gives me
$iso
[1] TRUE
$map
[1] 3 1 10 6 9 8 4 5
$maps
NULL
as a result. Supposedly $map contains the information I need. But I don't know how to use these numbers to generate a renaming of nodes from gsmall such that the renamed version of gsmall is actually a subgraph of gbig. I have the same translation problem with the output of subgraph_isomorphisms which according to the help returns a 'list of vertex sequences, corresponding to all mappings from the first graph to the second' which I don't understand.
Can anyone tell me how to get that renaming I want? If I am right with the assumption that the $map entry of the result of graph.subisomorphic.lad(gsmall, gbig, all.maps =F) contains what I need how can I get that renaming from that point on? If not how to achieve it in another way?
Thanks in advance.

unexpected behavior of replace

I need help with the replace() command
replace(c(3,2,2,1),1:3,4:6)
I was expecting an output of 6,5,5,4 but got 4,5,6,1
What am i doing wrong?
My understanding of what replace is this: it looks up index values of elements of the first argument in the second argument (e.g. 3 is the 3rd element in 1:3) and then replaces it with elements in the third argument with the same index (e.g. 3rd element in 4:6 is 6 thus the reason for me expecting the first element in the vector to be 6)
Thank you. (replace help file doesn't have example... need to ask for clarification here)
While replace doesn't give the behaviour your desired, to achieve what you were intending is quite easy to do using match:
new[match(x,i)]
It is all given in the description of replace(), just read carefully:
‘replace’ replaces the values in ‘x’ with indices given in ‘list’
by those given in ‘values’. If necessary, the values in ‘values’
are recycled.
x <- c(3, 2, 2, 1)
i <- 1:3
new <- 4:6
so this means in your case:
x[i] <- new
That command says to take the vector c(3, 2, 2, 1) and to replace the components with indices in 1:3 by the values given by the vector 4:6. This gives c(4, 5, 6, 1).

Resources