【发布时间】:2015-08-01 02:46:49
【问题描述】:
我有一个巨大的文件,我想在其中创建一个基于其他列的列。 我的文件如下所示:
person = c(1,2,3,4,5,6,7,8)
father = c(0,0,1,1,4,5,5,7)
mother = c(0,0,2,3,2,2,6,6)
ped = data.frame(person,father,mother)
我想创建一个列来指示此人是父亲还是母亲(性别列)。我在一个小例子中使用 for 循环得到它,但是当我在整个文件中应用时,需要几个小时才能完成。请问如何创建一个应用函数来解决这个问题。谢谢。
for(i in 1:nrow(ped)){
ped$test[i] = ifelse(ped[i,1] %in% ped[,2], "M", ifelse(ped[i,1] %in% ped[,3], "F", NA))
}
【问题讨论】: