[R] Levels and labels in factor

chong shiauyun shiauyun416 at hotmail.com
Wed Apr 10 10:00:40 CEST 2013


Hi R users, I have a imputed dataset of undefinedundefined cycles which I generated using StAta version undefinedundefined. Then I imported my data from Stata into R and I used a loop to run Mclust package in R. My observation starts with ID=2 (ID=1 has been excluded from the sample) and ends with 27950. Here is my code: 
library(mclust)
library(foreign)
dat<-read.dta(file="tempeundefined.dta")
impdat<-subset(dat,mim!=undefined)
datn<-impdat
apply(datn,undefined,range)
fix(datn)
mdlnc<-matrix(,undefinedundefined,undefined)
undefinedgetting the final output
n<-dim(datn)[undefined]
datf<-matrix(undefined,n,undefined)
for(i in undefined:undefinedundefined){
set.seed(undefinedundefinedundefinedundefinedundefinedundefined)
datnss <- subset(datn, mim==i)
datnssMclust<-Mclust(datnss[,undefined:undefinedundefined],model="VEV",G=undefined)
zv<-datnssMclust$z
clas<-datnssMclust$classification
zval<-cbind(zv,clas))
colnames(zval)<-c("Pundefined","Pundefined","Pundefined","class")
impd<-datnss[,c("cid_undefinedundefinedundefineda","qlet","mim")]
fd<-as.matrix(cbind(impd,zval))
datf[((undefinedundefinedundefinedundefinedundefined*(i-undefined)+undefined):(i*undefinedundefinedundefinedundefinedundefined)),]<-fd
}
cid_731a is my observation ID and mim is the number of imputed dataset. When I write the output in dta format (Stata data format), the IDs were reorganised. ID is now started with 1,2,3,4,...13797 which is not right. Label values have been attached to the existing data. The variables were now in long format. I guess that is because the factor in R is always begins with 1,2,3,4,... Is there anyway I can fix this? Please help 
SY 		 	   		  


More information about the R-help mailing list