[R] dummy coding problem

Jim Lemon jim at bitwrit.com.au
Wed Mar 26 22:36:55 CET 2014


On 03/27/2014 05: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") {
> 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
>
> Do you know where is wrong? many thanks!
>
Hi Si Qi,
It looks to me as though Employment.time$V1 is a column of a data frame 
and thus should be treated like a vector, not a matrix. If you want a 
dichotomous (two-valued) transformation of it, try this:

dummy<-Employment.time$V1 != "Over 4 years"

Jim




More information about the R-help mailing list