且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从数据框列表中的同一列中查找所有重复值并将其转换为NULL

更新时间:2022-02-04 16:18:46

在基础 R 中:

lapply(BELGIAN_COAST_list, function(x) {
  dups <- duplicated(x[, ncol(x)]) 
  x[dups, ncol(x)] <- NA_character_ 
  x})

这是在最后一列的位置完成的.如果要按名称调用该列,则可以将 ncol(x)更改为"Chemicals" .

This is done positionally, by the last column. If you want to call the column by name then you can change ncol(x) to "Chemicals".

使用 tidyverse :

library(tidyverse)

purrr::map(BELGIAN_COAST_list, ~ dplyr::mutate(., across(last_col(), ~ ifelse(duplicated(.), NA_character_, .))))

再次按列名进行调用,将 last_col()更改为 Chemicals :请注意此处缺少引号.

Again to call by column name change last_col() to Chemicals: note the lack of quotation marks here.

在任一情况下,如果 Chemicals 是数字,则使用 NA 代替 NA_character _ .

In either event, if Chemicals is numeric then use NA instead of NA_character_.