hi, I am a beginner of R. I want to create a 3 dimensional array but I can not define the length of each dimension. I am analysing students' marks of a class. there are 10 classes but it has different number of students. And "Grade" will be 100-90, 89-80, 79-70... until there will be any student who got the relevant marks. I was going to use a couple of for-loops with the multiple array. If I create a 3 dim array, arr<-array(data, dim=c(??,??,??)), dimnames= list("class","grade","average Marks") ) Can anyone shed some light on me, please?
+1
A:
You can't.
Array are of fixed dimensions. You could use the maximum along each dimension and allocate that along with NA
to fill in.
Lists are the alternative for varying lengths, and even different component types.
Dirk Eddelbuettel
2010-07-11 14:56:40
ah..yes, I think I've read something like 'fixed'dimension on array.. thank you.
grace
2010-07-11 15:10:58
+1
A:
You really do not want to use a matrix for this. A dataframe allows you to have a mixture of data types.
clasIDs <- c("Firsthour", "Secondhour", "Thirdhour")
class.size <-c(3, 5, 2) # small sizes for illustration
cls.frame <- data.frame(clasID=rep(clasIDs, class.size),
student.ID = unlist(sapply(class.size, function(x) seq(from=1, to=x))),
grade=factor(rep(NA,10) , levels=c("100-90", "89-80", "79-70")) )
> cls.frame
clasID student.ID grade
1 Firsthour 1 <NA>
2 Firsthour 2 <NA>
3 Firsthour 3 <NA>
4 Secondhour 1 <NA>
5 Secondhour 2 <NA>
6 Secondhour 3 <NA>
7 Secondhour 4 <NA>
8 Secondhour 5 <NA>
9 Thirdhour 1 <NA>
10 Thirdhour 2 <NA>
DWin
2010-07-11 15:16:22