I ´m getting the first steps in R and perhaps someone could help me. I have a table with n columns and n rows, and what I want to write a script to search each rows for a value, if don´t matches the value than it should proceed to the next row until if matchs the value. Once it matches the value it should go back to the previous row and the concatenate this row with the first column of the table. Can anyone give me any idea on how to make this on R?
Asked
Active
Viewed 258 times
2
-
1How big is your dataset? Can you please provide some [data](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), so we have something to work with? You should also show the desired output. – Roland Oct 08 '12 at 11:22
-
You can't (or really shouldn't) concatenate a row with a column. How are you envisioning the output array? Please give details on what your input is (in reality, not just "it's a matrix") and what you are trying to generate as an output. There is probably a much better way to solve the problem. – Carl Witthoft Oct 08 '12 at 12:37
1 Answers
1
Let's you are looking for the first occurrence of value X
in the table foo
. Try this:
i = min(which(foo==X, arr.ind=T)[,1])
if (i > 1) unlist(c(foo[i-1,], foo[,1]))
You may further remove the names of your result by unname()
command or assign your desired names by names()
.

Ali
- 9,440
- 12
- 62
- 92
-
-
@CarlWitthoft: I understand from the question that the desired value is not necessarily in the first column of foo. It should be searched within the entire rows. – Ali Oct 08 '12 at 12:46
-