1
   data(iris)
   library(RSNNS)
   library(clue)
   iris = iris[sample(1:nrow(iris),length(1:nrow(iris))),1:ncol(iris)]
   irisValues= iris[,1:4]
   irisTargets = decodeClassLabels(iris[,5])
   iris = splitForTrainingAndTest(irisValues, irisTargets, ratio=0.4)
   km<-kmeans(iris$inputsTrain,3)
   scorekm<-function(km, x,...)
   {
   data=NA
   args<-list(x,...)
   for(i in seq(from=1,to=nargs()-1,by=4))
   {
   data<-rbind(c(args[[i]],args[[i+1]],args[[i+2]],args[[i+3]]),data)
   }
   result<-cl_predict(km,data)
   result<-paste(result,";",sep="")
   print(result, quote=FALSE)
 }
scorekm(km,5.9,3.0,5.1,1.8,6.2,3.4,5.4,2.3,6.2,2.2,4.5,1.5 )
[1] 2;  3;  2;  NA;

Can anyone help delete the blank space between the numbers in the output and delete NA; in the output? I hope my output looks like this [1]2;3;2

Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
Will Wang
  • 105
  • 1
  • 3
  • 10
  • There you go: `[1]2;3;2;`. Please put more effort into your question, and I shall put more effort into my answer. – PascalVKooten Oct 26 '13 at 15:21
  • @Dualinity I'm sorry. I hope my output looks like this `[1]2;3;2` – Will Wang Oct 26 '13 at 15:24
  • Can you please `dput` the output from `scorekm` that you wish to trim. See [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) how you do that. Please also show what you have tried. Thanks! – Henrik Oct 26 '13 at 15:41
  • 1
    Additionally it seems a bit silly to have people who are helping you take additional time to remove the prompts (`>` and `+`) from your code. – Tyler Rinker Oct 26 '13 at 18:43
  • @TylerRinker.. here you go :) – Matt Bannert Oct 26 '13 at 18:58

1 Answers1

1

Hmm, though I really don't get the problem here I'll give it a try: The function scorekm looks like this.

scorekm <- function(km, x,...)
{
data=NA
args<-list(x,...)
for(i in seq(from=1,to=nargs()-1,by=4))
{
data<-rbind(c(args[[i]],args[[i+1]],args[[i+2]],args[[i+3]]),data)
}
result<-cl_predict(km,data)
result<-paste(result,";",sep="")
print(result, quote=FALSE)
}

As you can see it creates a character vector and prints it. So you could write this character vector to an R Object and then edit this object using, paste0 (with the collapse argument or gsub to replace " ". E.g. like this:

x <- scorekm(km,5.9,3.0,5.1,1.8,6.2,3.4,5.4,2.3,6.2,2.2,4.5,1.5 )
gsub("NA","",paste0(gsub(";","",x),collapse=";"))

Don't like the function's quality at all, NA doesn't seem to be a real NA, just a character sayin "NA" not a NA_character_ . Hence is.na() does not work.

Matt Bannert
  • 27,631
  • 38
  • 141
  • 207