[英]Replacing data frame cell value where column and row name is the same
I'm interested in finding a way to replace cells in an R data frame that have the same column and row name. 我对寻找一种方法来替换R数据框中具有相同列和行名称的单元格感兴趣。 This is because I'm comparing the minimum distance between spatial objects and it is not useful to compare objects to themselves.
这是因为我正在比较空间对象之间的最小距离,并且无法将对象与其自身进行比较。 For example, where both column name is "3" and row name is "3", the current value should be replaced with "NA".
例如,在列名均为“ 3”且行名均为“ 3”的情况下,应将当前值替换为“ NA”。
I've been playing around with if statements but think there must be a simpler way to do this that I'm overlooking. 我一直在使用if语句,但是认为必须有一种更简单的方法来执行此操作,而我却忽略了这一点。 I'm relatively new to coding so would appreciate sample code and an explanation.
我是编码的新手,所以希望您能欣赏示例代码和解释。
Using @SimonJackson's data: 使用@SimonJackson的数据:
d[cbind(seq_len(nrow(d)), match(rownames(d),colnames(d)))] <- NA
Simply match
the rownames
to the colnames
and then replace using a matrix index. 简单地
match
的rownames
到colnames
然后更换使用矩阵索引。
If the rownames and colnames are in the same order in a square matrix/data.frame, then just: 如果行名和列名在方阵/data.frame中的顺序相同,则:
diag(d) <- NA
...will do it. ...会做的。
Here's a custom function, replace_matching()
to do the job. 这是一个自定义函数
replace_matching()
来完成这项工作。 It will even let you adjust what to insert in the cells: 它甚至可以让您调整要插入单元格的内容:
Here's some toy data... 这是一些玩具数据...
d <- data.frame(a = 1:10)
for (i in 2:10) {
d[[letters[i]]] <- 1:10
}
rownames(d) <- letters[10:1]
d
#> a b c d e f g h i j
#> j 1 1 1 1 1 1 1 1 1 1
#> i 2 2 2 2 2 2 2 2 2 2
#> h 3 3 3 3 3 3 3 3 3 3
#> g 4 4 4 4 4 4 4 4 4 4
#> f 5 5 5 5 5 5 5 5 5 5
#> e 6 6 6 6 6 6 6 6 6 6
#> d 7 7 7 7 7 7 7 7 7 7
#> c 8 8 8 8 8 8 8 8 8 8
#> b 9 9 9 9 9 9 9 9 9 9
#> a 10 10 10 10 10 10 10 10 10 10
The function... 功能...
replace_matching <- function(df, replace_with = NA) {
# Find all combinations of row and column names
name_grid <- expand.grid(rownames(df), colnames(df), stringsAsFactors = FALSE)
# Find any names with matches
matched_names <- name_grid$Var1[name_grid$Var1 == name_grid$Var2]
# Wherever there are matches, set cell to NA
for (mn in matched_names) {
df[mn, mn] <- replace_with
}
return(df)
}
Default use case... 默认用例...
replace_matching(d)
#> a b c d e f g h i j
#> j 1 1 1 1 1 1 1 1 1 NA
#> i 2 2 2 2 2 2 2 2 NA 2
#> h 3 3 3 3 3 3 3 NA 3 3
#> g 4 4 4 4 4 4 NA 4 4 4
#> f 5 5 5 5 5 NA 5 5 5 5
#> e 6 6 6 6 NA 6 6 6 6 6
#> d 7 7 7 NA 7 7 7 7 7 7
#> c 8 8 NA 8 8 8 8 8 8 8
#> b 9 NA 9 9 9 9 9 9 9 9
#> a NA 10 10 10 10 10 10 10 10 10
Changing default replacement value to -99... 将默认替换值更改为-99 ...
replace_matching(d, -99)
#> a b c d e f g h i j
#> j 1 1 1 1 1 1 1 1 1 -99
#> i 2 2 2 2 2 2 2 2 -99 2
#> h 3 3 3 3 3 3 3 -99 3 3
#> g 4 4 4 4 4 4 -99 4 4 4
#> f 5 5 5 5 5 -99 5 5 5 5
#> e 6 6 6 6 -99 6 6 6 6 6
#> d 7 7 7 -99 7 7 7 7 7 7
#> c 8 8 -99 8 8 8 8 8 8 8
#> b 9 -99 9 9 9 9 9 9 9 9
#> a -99 10 10 10 10 10 10 10 10 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.