################### HOW TO USE ################### # This function distributes items in a data frame to blocks/lists by latin square. It assumes a design with a 1:1:1 ratio of conditions to lexicalizations to output lists/blocks, so the function takes only one argument corresponding to these values. To use this script, read in the code below. The usage and description of arguments are as follows: # lsquare(n.blocks, d.frame, csv=FALSE) # The first argument, n.blocks, is the numbers of blocks to be created. This must be the same as the number lexicalizations in the dataframe and the same number of conditions. #The second argument, d.frame, is the object with containing the items to be blocked. The script distributes items to blocks by the item's row number in the data frame, so the data frame will need to be organized with experimental items grouped by by lexicalization and with conditions in the same order for each lexicalization. An example data file for a 2x3 design (6 lexicalizations) is at: http://qcpages.qc.cuny.edu/~whaddican/norwegian.csv. #The final argument, csv, is optional. If TRUE, it writes a .csv for the output to the working directory. lsquare<-function(n.blocks, d.frame, csv=FALSE){ # Take out headers for the time being. izenak<-names(d.frame) nam<-"V1" for(i in 2:length(d.frame)){ nam<-append(nam, paste0("V", i), after=length(nam)) } colnames(d.frame)<-nam # Make sure all columns are factors. for(i in 1:length(d.frame)){ d.frame[,i]<-as.factor(d.frame[,i]) } # Create blocks. for(i in 1:n.blocks){ file<-matrix(c(1), 1, ncol(d.frame)) file[1,]<-paste("Block", i, sep=" ") assign(paste0("Block", i), file) rm(file) } rm(i) # Assign sentences to blocks. for (lex in 1:n.blocks){ lex.counter<-(lex*n.blocks)-(n.blocks) for (blocks in 1:n.blocks){ blocks.counter<-blocks+lex-1 if(blocks.counter>n.blocks){blocks.counter<-(blocks.counter-n.blocks)} holder<-d.frame[lex.counter+blocks, ] assign(paste0("Block", blocks.counter), rbind(get(paste0("Block", blocks.counter)), holder)) } } # Bind them together. Blockfile<-Block1 for (blocks in 2:n.blocks){ Blockfile<-rbind(Blockfile, get(paste0("Block", blocks))) } # Stick back in column names. columns<-c(1:ncol(d.frame)) c.number<-columns for(i in 1:length(columns)){ number<-columns[i] nam<-paste("Column", c.number[i], sep=" ") c.number[i]<-nam } colnames(Blockfile)<-izenak print(Blockfile) # Maybe write a csv file. if(csv==TRUE){ write.csv(Blockfile, "Blockfile.csv") } }