Embed two-dimensional arrays as part of higher-dimensional arrays in R
My questions concerns how to cleanly deal with a bunch of data I have.
I'm running an experiment with 4 conditions. There will be 20 participants in each condition, and when each completes the experiment, I'm left with a text file that has 600 rows, and 7 columns. The 600 rows correspond to 600 trials that each person completes. The 7 columns refer to variables that are measured on each trial.
So my data for each person look something like this (except there are 600 rows):
394 b a 0 9773 1 1436
114 a b 0 3595 1 1246
432 b a 0 1272 1 1061
209 a a 1 3514 1 120
In order to run my analyses, it would be really helpful if I could get all these text files into the one object called "data" that would have the following dimensions:
- Experimental condition (1-4)
- Participant number (1-20)
- Trial number (1-600)
- Variable (1-7)
My files have names like "ii-wm-long_1316994934_7_1.txt", where the "ii-wm-long" part identifies their experimental condition, and the last number (1 here) identifies their participant number.
At the moment my code looks like this:
#Get the names of the text files in the results folder
files <- list.files()
#Conditions - which ones correspond to which numbers
condition.def <- c("开发者_如何学Cii-wm-long","ii-wm-short","wm-ii-long","wm-ii-short")
#1 = ii-wm-long
#2 = ii-wm-short
#3 = wm-ii-long
#4 = wm-ii-short
#This is where everything will be stored
data <- array(NA,dim=c(4,20,600,7),dimnames=c("condition","participantNumber","trialNumber","experimentalvariable"))
#Loop for each participant's file
for (n in 1:length(files)){
#What condition is the person in?
condition <- unlist(strsplit(files[n],"\\_"))[1]
condition <- grep(condition,condition.def)
#What is their participant number (of the people in that condition)?
ppt <- as.integer(unlist(strsplit(unlist(strsplit(files[n],"\\_")),"\\."))[4])
#Read the text file into the array
data[condition,ppt,,] <- read.table(files[n],sep="\t",header=F,nrows=600,col.names=c("stimulus","category","category.choice","category.correct","category.time","memory.present","memory.time"))
}
I receive an error:
Error in data[condition, ppt, , ] <- read.table(files[n], sep = "\t", :
incorrect number of subscripts
I've read up on cbind and abind, and can't seem to figure out how they would allow me to read the data in one-by-one.
What is the correct way to take a 2D array, and turn it into the last 2 dimensions of a 4D array?
read.table
returns a data.frame
, so at the very least you would need to wrap it in as.matrix
:
data[condition,ppt,,] <-
as.matrix(read.table(files[n], sep="\t", header=FALSE, nrows=600,
col.names=c("stimulus", "category", "category.choice",
"category.correct", "category.time",
"memory.present", "memory.time")))
But, this is pretty fragile since you are going directly from I/O into an array slice, and you should put in some sanity checks so you know the input is as expected, the file exists, and so on.
精彩评论