Constantinos Antoniou
2005-Jul-09 10:53 UTC
[R] getting a variable from an object named using paste
Hello, The subject could be articulated better ;(, but I am stuck... In any case, my problem is the following: I am trying to use knn, and it requires a classification. So I am using cmeans for this. What I want to do is add the classification as a new column in my data.frame (to be used for knn). Now, the trick is that I would like to name the output of the cmeans classification based on the number of clusters (e.g. cmeans30 for 30 clusters). To do this, I do the following: [The file for this example can be downloaded from: http://mit.edu/costas/www/station-1-120103.txt ] library(e1071) lala <- read.csv("station-1-120103.txt",header=F) ll <- cbind(lala$V25,lala$V22) num.of.clust=50 assign(paste("cmeans",num.of.clust,sep=""),cmeans(ll,num.of.clust)) and sure enough if I type cmeans50 I get the output of this cmeans run. This also works, naturally: llc<-cbind(ll,cmeans50$cluster) [and I now get three columns, where the third column is the cluster ID.] However, when I do what I really want (i.e. not call cmeans50 explicitly, but through the name that I constructed programmatically): llc <- cbind(ll,(paste("cmeans",num.of.clust,sep=""))$cluster) I only get two columns (the last term has no effect). Any ideas as to what I am doing wrong? Thanks a lot! Costas -- Constantinos Antoniou, Ph.D. Department of Transportation Planning and Engineering National Technical University of Athens 5, Iroon Polytechniou str. GR-15773, Athens, Greece
Constantinos Antoniou
2005-Jul-09 11:01 UTC
[R] getting a variable from an object named using paste
Hello, The subject could be articulated better ;(, but I am stuck... In any case, my problem is the following: I am trying to use knn, and it requires a classification. So I am using cmeans for this. What I want to do is add the classification as a new column in my data.frame (to be used for knn). Now, the trick is that I would like to name the output of the cmeans classification based on the number of clusters (e.g. cmeans30 for 30 clusters). To do this, I do the following: [The file for this example can be downloaded from: http://mit.edu/costas/www/station-1-120103.txt ] library(e1071) lala <- read.csv("station-1-120103.txt",header=F) ll <- cbind(lala$V25,lala$V22) num.of.clust=50 assign(paste("cmeans",num.of.clust,sep=""),cmeans(ll,num.of.clust)) and sure enough if I type cmeans50 I get the output of this cmeans run. This also works, naturally: llc<-cbind(ll,cmeans50$cluster) [and I now get three columns, where the third column is the cluster ID.] However, when I do what I really want (i.e. not call cmeans50 explicitly, but through the name that I constructed programmatically): llc <- cbind(ll,(paste("cmeans",num.of.clust,sep=""))$cluster) I only get two columns (the last term has no effect). Any ideas as to what I am doing wrong? Thanks a lot! Costas -- Constantinos Antoniou, Ph.D. Massachusetts Institute of Technology Intelligent Transportation Systems Program 77 Massachusetts Ave., Rm. 1-249, Cambridge, MA 02139
Constantinos Antoniou wrote:> Hello, > > The subject could be articulated better ;(, but I am stuck... > > In any case, my problem is the following: I am trying to use knn, and > it requires a classification. So I am using cmeans for this. What I > want to do is add the classification as a new column in my data.frame > (to be used for knn). Now, the trick is that I would like to name the > output of the cmeans classification based on the number of clusters > (e.g. cmeans30 for 30 clusters). To do this, I do the following: > > > [The file for this example can be downloaded from: > > http://mit.edu/costas/www/station-1-120103.txt ] > > > library(e1071) > lala <- read.csv("station-1-120103.txt",header=F) > ll <- cbind(lala$V25,lala$V22) > num.of.clust=50 > assign(paste("cmeans",num.of.clust,sep=""),cmeans(ll,num.of.clust)) > > and sure enough if I type cmeans50 I get the output of this cmeans run. > > This also works, naturally: > > llc<-cbind(ll,cmeans50$cluster) > > [and I now get three columns, where the third column is the cluster ID.] > > However, when I do what I really want (i.e. not call cmeans50 > explicitly, but through the name that I constructed programmatically): > > llc <- cbind(ll,(paste("cmeans",num.of.clust,sep=""))$cluster)This is a FAQ: use get() Uwe Ligges> I only get two columns (the last term has no effect). > > Any ideas as to what I am doing wrong? > > Thanks a lot! > > Costas > > >
Constantinos Antoniou
2005-Jul-09 14:44 UTC
[R] getting a variable from an object named using paste
Indeed. Thanks and sorry, Costas PS. also, sorry about sending the original post twice... I did not want to pollute the list any more with an apology then... On 09 佄檨蝸縼蠀佄伝 2005, at 3:30 佄渷螠, Uwe Ligges wrote:> Constantinos Antoniou wrote: > >> Hello, >> The subject could be articulated better ;(, but I am stuck... >> In any case, my problem is the following: I am trying to use knn, >> and it requires a classification. So I am using cmeans for this. >> What I want to do is add the classification as a new column in my >> data.frame (to be used for knn). Now, the trick is that I would >> like to name the output of the cmeans classification based on the >> number of clusters (e.g. cmeans30 for 30 clusters). To do this, I >> do the following: >> [The file for this example can be downloaded from: >> http://mit.edu/costas/www/station-1-120103.txt ] >> library(e1071) >> lala <- read.csv("station-1-120103.txt",header=F) >> ll <- cbind(lala$V25,lala$V22) >> num.of.clust=50 >> assign(paste("cmeans",num.of.clust,sep=""),cmeans(ll,num.of.clust)) >> and sure enough if I type cmeans50 I get the output of this cmeans >> run. >> This also works, naturally: >> llc<-cbind(ll,cmeans50$cluster) >> [and I now get three columns, where the third column is the >> cluster ID.] >> However, when I do what I really want (i.e. not call cmeans50 >> explicitly, but through the name that I constructed >> programmatically): >> llc <- cbind(ll,(paste("cmeans",num.of.clust,sep=""))$cluster) >> > > This is a FAQ: use get() > > Uwe Ligges > > > >> I only get two columns (the last term has no effect). >> Any ideas as to what I am doing wrong? >> Thanks a lot! >> Costas >> > > >-- Constantinos Antoniou, Ph.D. Massachusetts Institute of Technology Intelligent Transportation Systems Program 77 Massachusetts Ave., Rm. 1-249, Cambridge, MA 02139