我正在使用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