0
說,我有一個數據框,我需要對其單元格做些事情,並記住我已更改的單元格。一種方法是通過兩個for-loop循環索引。但是有沒有辦法用一個循環來做到這一點?R:在通過df元素循環時獲取索引名稱
完美,我需要這樣的:
changes = data.frame(Row = character(), Col = character())
for (cell in df){
if (!(is.na(df))){
cell = do.smt(cell)
temp = list(Row = get.row(cell), Col = get.col(cell))
changes = rbind(changes,temp)
}
}
例的什麼,我需要:
df = data.frame(A = c(1,2,3), B = c(4,5,6), C = c(7,8,9))
rownames(df) = c('a','b','c')
changes = data.frame(Row = NA, Col = NA)
for (i in rownames(df)){
for (j in colnames(df)) {
if (df[i,j] > 5) {
df[i,j] = 0
temp = list(Row = i, Col = j)
changes = rbind(changes, temp)
}
}
}
也許與最終的DF比較初始DF。如果您的df是數字,則減去兩者並檢查哪些條目不等於0.您應該給我們提供您的數據樣本。什麼是smt? – Ansjovis86
請給出一個有意義的最小工作示例和預期結果。我懷疑這可以在1行代碼中解決(使用向量化方法)。 –
@RomanLuštrik添加了示例 – lotrus28