Reputation: 21
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 in a class. There are 10 classes but it has a 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") )
Upvotes: 2
Views: 2911
Reputation: 263301
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>
Upvotes: 4
Reputation: 368181
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.
Upvotes: 2