On 09.06.2011 14:31, Dominik P.H. Kalisch wrote:
Hi Uwe,

thanks for your help. I didn't know that function (mea culpa)...
With merge() it works what I want to do.
But I'm still interessted, for a learning purpose, why the loop didn't
work. So here are the str() results:

str(cluster)
  int [1:18, 1:2] 12051000 12052000 12053000 12054000 12060000 12061000
12062000 12063000 12064000 12065000 ...
  - attr(*, "dimnames")=List of 2
   ..$ : chr [1:18] "1" "2" "3" "4" ...
   ..$ : chr [1:2] "Kreis" "Cluster"

str(KreisSA)
  num [1:2302, 1:2] 12069000 12072000 12067000 12060000 12070000 ...
  - attr(*, "dimnames")=List of 2
   ..$ : NULL
   ..$ : chr [1:2] "Kreis" "Cluster"


So KreisSA is a 2 column matrix rather than a vector. Hence you also get 2 column matrix (logical) from the comparison KreisSA == cluster[i,1] and so you are assigning into KreisSAa.

Uwe Ligges



Thanks for your help.


On Tue, 07 Jun 2011 22:49:04 +0200, Uwe Ligges
<lig...@statistik.tu-dortmund.de>  wrote:
On 07.06.2011 16:24, Dominik P.H. Kalisch wrote:
Hi,

I have two matrices of the following form:

cluster (n=18):
12062 1
12063 2
12064 2
12065 3
12066 5

KreisSA (n=2304)
12062
12062
12067
12065
12063
12067

I try to assign the cluster[,2] to KreisSAa by the follwoing loop:

n<- nrow(cluster)
KreisSAa<- numeric()

for(i in 1:n){
KreisSAa[KreisSA == cluster[i,1]]<- cluster[i,2]
}

The result is a vector of the length n=4608 where after the entry 2304
are just NA's
Has someone an idea what I do wrong?

Since we do not know the structure of your objects, we cannot say
easily. You may want to provide

str(cluster)
and
str(KreisSA)

Anyway: I'd just use merge()

Uwe Ligges






Thanks for your help.
Dominik

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to