简体   繁体   English

替换列名和行名相同的数据帧单元格值

[英]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. 简单地matchrownamescolnames然后更换使用矩阵索引。

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.

相关问题 根据同一列中的其他行值替换 R 数据框中的行值 - Replacing row values in R data frame based on other row value in same column 返回单元格值与模式匹配的 R 数据框的行和列位置 - Return the row and column position of R data frame where the cell value matches a pattern 如何在数据框中行追加具有相同列名的列 - How To Row Append Column With Same Column Name In A Data Frame 从数据框中收集列和行中具有相同名称的值 - Collecting the value which have the same name in column and row from data frame R:根据数据框中的值,用行名替换列名 - R: Replace Column Name with Row Name on Bases of a Value in Data Frame 如何在R中的列名相同的数据帧中创建rowMeans - How to make rowMeans of data frame where column name is same in r 如果另一个列值与 R 中另一个数据框中的相应单元格不匹配,则将数据框中的列值替换为 null - Replacing column values with null in a data frame if another column value doesn't match with the corresponding cell in another data frame in R 用R数据帧中的一个值替换多个单元格(各种行/列组合) - Replacing multiple cells (various row/column combinations) with one value in R data frame 从一个列/行值等于列名的单元格中提取值 - Extract value from cell where one column/row value is equal to the column name R:将数据框x中的行名与数据框y中的列名匹配,以使两个数据框共享的列中的给定变量值 - R: match row name in data frame x with column name in data frame y for a given variable value in column shared by the two data frames
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM