我正在使用R来尝试将for循环的结果写入到我在数据框中创建的列中。我的额外列是hetCSF在我的数据框中的基因型,我试图给它分配一个默认值0. for循环应该比较我的数据框中的每个1036行的2列,如果两个数据条目返回值1是相同的,如果不是,则为0。有人可以看看这段代码,告诉我我做错了什么吗?将for循环写入列的结果
Heterozygosity <- function (Genotypes$CSF1PO, Genotypes$CSF1PO.1){
Genotypes$hetCSF <- 0 #gives Genotypes$hetCSF a default value of 0 which corresponds to heterozygous
for (i in 1:nrow(Genotypes)){ #loops the following across all rows
Genotypes$hetCSF <- as.numeric(identical(Genotypes[i, "CSF1PO"], Genotypes[i, "CSF1PO.1"])) #decides if the 2 columns have the same value and are therefore homozygous, returns 1 for homozygous in new column homCSF.G
}
}
目前,当我运行它时,它告诉我我有一个unexpected '$' in "Heterozygosity <- function Genotypes$" and an unexpected '}' in "}"
。非常感谢你的帮助。我对R很新,所以我很抱歉,如果这是一个非常基本的问题。
非常感谢您,它的工作方式就像一个魅力!我之前并不熟悉转换函数。 – user3083973