[R] Odp: stack data sets

Petr PIKAL petr.pikal at precheza.cz
Thu Jan 29 08:50:03 CET 2009


Hi

r-help-bounces at r-project.org napsal dne 28.01.2009 19:25:32:

> 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?

It is rather difficult to understand what you want to do. The code you 
provide do not work as I presume only you have C:/NCME path and your data.

Stack works with data frames as you point out. However if your matrix has 
appropriate form and names you can easily transform it to data frame and 
use stack.


pg <- unstack(PlantGrowth)
pg<-as.matrix(pg)
pg
      ctrl trt1 trt2
 [1,] 4.17 4.81 6.31
 [2,] 5.58 4.17 5.12
...
 [9,] 5.33 4.32 5.80
[10,] 5.14 4.69 5.26
> stack(pg)
Error in rep.int(names(x), lapply(x, length)) : invalid 'times' value
> as.data.frame(pg)
   ctrl trt1 trt2
1  4.17 4.81 6.31
2  5.58 4.17 5.12
...
9  5.33 4.32 5.80
10 5.14 4.69 5.26
> stack(as.data.frame(pg))
   values  ind
1    4.17 ctrl
2    5.58 ctrl
3    5.18 ctrl
4    6.11 ctrl
5    4.50 ctrl
6    4.61 ctrl
....

Regards
Petr



> 
> 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
> 
> ______________________________________________
> 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.



More information about the R-help mailing list