[R-sig-eco] Association Routine?

Alexandre F. Souza alexsouza.cb.ufrn.br at gmail.com
Sat Feb 28 18:48:01 CET 2015


Dear friends,

I need to write a code to find data using one variable as reference. The
code I wrote, however, is not working and I can't figure it out why. Could
anyone help me?

Imagine a data set with two variables, B and C. Now I have variable A,
which is the same variable as variable B but the data are not in the same
order nor have necessarily the same extension as B (it may be a sample of
B, for example).

I want to find the values of variable C that match each line in variable A
using B as the association criterion. So the code should perform a loop in
which it would take the first line in A, search B until it finds it there,
then copy the corresponding value of C and store it in a new variable D. Do
it until all lines in A have been associated to a C value.

Here is the code I wrote:


# Considering that matrices data.ref and data.assoc have been already read,
containing the

# User-defined number of columns to be associated with A (I imagined that
more than one variable could be associated at once)
col.assoc = 20

# To assure that data will not be in a non-usable data category
ref = as.matrix(data.ref)
assoc = as.matrix(data.assoc)


# Table where results will be stored
#  Number of columns = n associated variables plus one column
#  Reserved to receive the initial data (example column A)

result = matrix(nrow = nrow(ref), ncol = col.assoc + 1)

# Fulfill the first column of the result table with the original reference
variable

result[,1] = ref[,1]


for (i in 1:nrow(ref)){
  for (j in 1:nrow(assoc))
   if (ref[i, 1] == assoc[j, 1]){
     resultado[i, 2] == assoc[j, 2]
   }
}



col = ncol(dados)

####

Any thoughts?

Thanks in advance,

Alexandre

-- 
Dr. Alexandre F. Souza
Professor Ajunto II
Universidade Federal do Rio Grande do Norte
CB, Departamento de Ecologia
Campus Universitário - Lagoa Nova
59072-970 - Natal, RN - Brasil
lattes: lattes.cnpq.br/7844758818522706
http://www.docente.ufrn.br/alexsouza

	[[alternative HTML version deleted]]



More information about the R-sig-ecology mailing list