Hello, I wish to carry out the following: factor_classes <- as.factor(sample(c("BM", "SM", "F"), 10, replace = TRUE)) C <- 5 k <- matrix(0, length(factor_classes), 1) for (i in length(factor_classes)) { k[i] <- C*factor_classes[i] } I get the following error message: non-numeric argument to binary operator I wish to store the results of C*factor_classes[i] in matrix k. Is there any way I can multiply the factor with the numeric variable? Thanks for your help in advance. Cheers, sumaria [[alternative HTML version deleted]]
Hello, Try C*as.integer(factor_classes) And note that you don't need the loop. Hope this helps, Rui Barradas Em 16-12-2012 23:56, Shrupa Sumaria escreveu:> Hello, > > > I wish to carry out the following: > > > factor_classes <- as.factor(sample(c("BM", "SM", "F"), 10, replace = TRUE)) > > > C <- 5 > > > k <- matrix(0, length(factor_classes), 1) > > > for (i in length(factor_classes)) { > > > k[i] <- C*factor_classes[i] > > > } > > > I get the following error message: > > non-numeric argument to binary operator > > > I wish to store the results of C*factor_classes[i] in matrix k. > Is there any way I can multiply the factor with the numeric variable? > > > Thanks for your help in advance. > > > Cheers, > > sumaria > [[alternative HTML version deleted]] > > > > ______________________________________________ > 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.[[alternative HTML version deleted]]
On 12-12-16 6:56 PM, Shrupa Sumaria wrote:> Hello, > > > I wish to carry out the following: > > > factor_classes <- as.factor(sample(c("BM", "SM", "F"), 10, replace = TRUE)) > > > C <- 5 > > > k <- matrix(0, length(factor_classes), 1) > > > for (i in length(factor_classes)) { > > > k[i] <- C*factor_classes[i] > > > } > > > I get the following error message: > > non-numeric argument to binary operator > > > I wish to store the results of C*factor_classes[i] in matrix k. > Is there any way I can multiply the factor with the numeric variable?What do you expect to get from 5*"BM"? Duncan Murdoch> > > Thanks for your help in advance. > > > Cheers, > > sumaria > [[alternative HTML version deleted]] > > > > ______________________________________________ > R-help at 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. >