[R] stack data sets

Nidhi Kohli nidhik at umd.edu
Wed Jan 28 19:25:32 CET 2009


Hi All,

I'm generating 10 different data sets with 1 and 0 in a matrix form and writing the output in separate files. Now I need to stack all these data sets in one vector and I know that stack only operates on list or data frame however I got these data sets by converting list to a matrix so can't go backwards now. Is there a way i can still use Stack?

Please see the program:

#Importing psych & ltm library for all the simulation related functions
library(ltm)
library(psych)
# Settting the working directory path to C:/NCME
path="C:/NCME"
setwd(path)
#IRT Data Simulation Routine#
n.exams = 500   #Sets number of examinees to be generated#
n.items = 20     #Sets number of items to be generated#
#The following intialize empty (NA) vectors or matrices#
beta.values = rep(NA,n.items)
resp.prob=matrix(rep(NA, n.exams*n.items), nrow=n.exams, ncol=n.items)
Observed_Scores=matrix(rep(NA, n.exams*n.items), nrow=n.exams, ncol=n.items)
str(Observed_Scores)
for (k in 1:10)
{
#Setting the starting point for seed
set.seed(k)
#filling item parameters into beta.values
beta.values = runif(n.items,-2,2)
#Calculating Threshold
thresh.values = .5 * beta.values

#Using the function to generate the Parallel Model CTT data
GenData <- congeneric.sim(N=500, loads = rep(.5,20), err=NULL, short = FALSE)

#Storing Observed Score in a variable
Observed_Scores = GenData[[3]]
#Exporting Observed scores to output file
ObservedScores_Data <- paste("Observed_Scores_",k,".dat")
write.table(Observed_Scores,ObservedScores_Data,row.name=FALSE,col.name=FALSE)
Zero = 0
One = 1
for (t in 1:20)
{
for (s in 1:500)
{
if (Observed_Scores[s,t]<= thresh.values[t])
resp.prob[s,t] = Zero
else
resp.prob[s,t] = One
 
}
}
ResponseData <- paste("ResponseMatrix_",k,".dat")
ThreshData <- paste("Threshold_",k,".dat")
write.table(resp.prob,ResponseData,row.name=FALSE,col.name=FALSE)
write.table(thresh.values,ThreshData,row.name=FALSE,col.name=FALSE)

#####STACKING ALL THE OUTPUTS#########
CommonFile <- stack(resp.prob)
######################################

#Rounding upto 2 decimal places while showing the correlation matrix
round(cor(GenData$observed),2)
#Factor Score
FactorScore=factor.pa(GenData$observed,1,scores = "TRUE")
round(cor(FactorScore$scores,GenData$latent),2)
filename_fs <- paste("FactorScore_",k,".dat")
#Exporting Factor Scores to Output file
write.table(FactorScore$scores,filename_fs,col.name=FALSE, row.name=FALSE)
}


Thank you
Nidhi




More information about the R-help mailing list