我正在尝试使用标识符和其他一些参数以及每个班级的学生和老师来定义每个学校中带有标识符和其他参数和课程的学校。我为每种类型定义了S4对象。即
student <- setClass(
"student",
slots = c(
identifier = "numeric",
testscore = "numeric",
endAbil = "numeric"
)
)
clss <- setClass(
"class",
slots = c(
identifier = "numeric",
teacher = "teacher",
student = "student",
capacity = "numeric"
)
)
school <- setClass(
"school",
slots = c(
identifier = "numeric",
cls = "matrix",
capacity = "numeric"
)
)
我的问题是,如果我改变学生的价值,我会随机将学生分配到学校和班级,这只会改变学校内部的价值。有没有办法将这些价值观与学生联系起来,如果我们改变第i学校的学生变量的价值,它也会改变学生的变量。
Students <- new("student", identifier = sample('#ofStudents'))
Students@endAbil <-rnorm('#ofStudents',mean = 0 , sd = 1)
# Randomly assign students to classes
ind = 1:'#ofStudents'
# #ofClasses is fixed for all schools
for (i in 1:'#ofSchools'){
for (j in 1:'#ofClasses'){
# get the ith schools' jth class
tmp <- sample(ind,Schools@cls[i,j][[1]]@capacity)
Schools@cls[i,j][[1]]@student@identifier <- Students@identifier[tmp]
Schools@cls[i,j][[1]]@student@endAbil <- Students@endAbil[tmp]
ind = ind [! ind %in% tmp]
}
}
答案 0 :(得分:0)
R具有更改语义而不是引用语义的副本,因此您希望执行的操作通常不是首选或“自然”方式。您可以调查所谓的引用类或环境,但最好将每个类视为描述规范化数据库表的列。更新相应表中的值,而不是建模复杂的嵌套关系。