-2

Apologies if this is the wrong title, I couldn't think what else to call it.

In matlab I was able to do the following:

x = (2,7,8)
var1 = (1:1:10,1)
var2 = var1(x,1) 

Therefore it would create a new array. containing the values 2,7 and 8.

I am trying to replicate this process in R but I am struggling profusely and can't think how to word the question properly.

Any help would be much appreciated.

EDIT:

I am trying to create a new array, var2 by doing the following:

x = (2,7,8)

var1 = (1,2,3,4,5,6,7,8,9,10)

In matlab I could type the following:

var2 = var1(x,1)

Thus:

var2 = (2,7,8)

I am looking how to do this in R, using a defined variable to find the corresponding row numbers basically..

Infernez
  • 73
  • 7
  • Also add expected result – Sotos Apr 18 '18 at 09:17
  • What does `var1(x,1)` do in matlab ? – Ronak Shah Apr 18 '18 at 09:19
  • In matlab `var1(x,1)` will make a new array for me with those variables in the defined `x`. – Infernez Apr 18 '18 at 09:21
  • It's still not clear to me but I think by `using a defined variable to find the corresponding row numbers` you may need `match(x, var1)` which returns matches of positions of first argument in second. – Ronak Shah Apr 18 '18 at 09:25
  • Hmmm, let me see if I can explain one more time. `var1 = 1 - 10` as described. `x=2,7,8`. I think in R it will go something like this: `var2 = var1(x,1)` resulting in `var2 = ([2,7,8],1)`. Thus taking only those row numbers as defined by the `x` variable. As I said, i was not really sure how to ask the question correctly. I hope this clears it up. – Infernez Apr 18 '18 at 09:32

1 Answers1

0

Your question is not very clear, if I corretly understand the problem, try to use this code:

x = c(2,7,8)
var1 = c(1:1:10,1)
var2 = var1[var1 %in% x]
var2
[1] 2 7 8
Terru_theTerror
  • 4,918
  • 2
  • 20
  • 39
  • Correct me if I am wrong, but this finds the row location of the numbers defined by x? – Infernez Apr 18 '18 at 09:20
  • Is the match between the values in var1 and x, what are you looking for? – Terru_theTerror Apr 18 '18 at 09:22
  • I am to blame. I was just wondering how to make `var2` in a similar way. I have an array exponentially larger, and I am trying to take the readings at the rows as defined by your `var2` solution. – Infernez Apr 18 '18 at 09:28
  • Why not var2<-x? – Terru_theTerror Apr 18 '18 at 09:45
  • Because in my larger array, I have 30K + of specific rows I would like to extract so I need: `var2 <- var1(x,1)` – Infernez Apr 18 '18 at 10:00
  • I was hoping the solution above would allow me to do the following: I have four variables in a data frame`Df = data.frame(var1 var2 var3 var4)` they are all the same length and 1 column, we shall say the dims are 30,000 (rows) x 1 (column). I have found the locations of my errors with this code: `ErLoc1 = which(Var2 %in% Error)` This gives me the row numbers where the errors occur i.e. the location. I then want to apply this to `Df` So then I have `Df2` which will only be formed of the rows indicated from `ErLoc1`. I hope this is clearer. – Infernez Apr 18 '18 at 14:06
  • Please, read this https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and improve your question with the above details and other details needed. – Terru_theTerror Apr 18 '18 at 14:21
  • I was just stating what the solution to the above question would enable me to do. In Matlab I believe it would. – Infernez Apr 18 '18 at 14:34