[R] dummy coding problem

David Winsemius dwinsemius at comcast.net
Wed Mar 26 22:44:03 CET 2014


On Mar 26, 2014, at 11:34 AM, Si Qi L. wrote:

> Hi, I have got a problem with dummy coding and I really couldn't figure it
> out. Would you please help me out? this is my codes:
> 
> idx<-sort(unique(Employment.time$V1));
> dummy <- matrix(NA, nrow=nrow(Employment.time), ncol= length(idx))
> 
> for (i in 1:nrow(Employment.time)) {
> 
> for (j in 1:length(idx)) {
> 
> if (Employment.time$V1[i,j] == "Over 4 years") {

The problem in coding (aside from the conceptual misunderstandings identified by Bert) are that there simply cannot be an [i,j] index to a column vector. Perhaps you will loose the error with 

Employment.time$V1[j] == "Over 4 years"

... but there are surely more compact ways of expressing this in R, if you would only provide a better description of the data and what you are trying to do.

-- 
David.

> dummy[i,j] <- 0
> }
> else {
> dummy[i,j] <- 1
> }
> }
> }
> 
> but the R shows that
> 
> Error in `[.default`(Employment.time$V1, i, j) :
> incorrect number of dimensions

That vector doesn't actually have dimensions (at least in R's meaning for the word). Hence the error.

> 
> Do you know where is wrong? many thanks!
> 
> 	[[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.

David Winsemius
Alameda, CA, USA




More information about the R-help mailing list