当前位置:网站首页>dplyr 中的filter报错:Can‘t transform a data frame with duplicate names
dplyr 中的filter报错:Can‘t transform a data frame with duplicate names
2022-06-30 10:51:00 【育种数据分析之放飞自我】
背景
处理一个数据框,用filter进行筛选数据时,发现了这个报错:
aa1 %>% filter(POS_strt == “20696”)
Error infilter():
! Can’t transform a data frame with duplicate names.
Runrlang::last_error()to see where the error occurred.
根据提示运行:rlang::last_error(),也没有看到问题所在:
> rlang::last_error()
<error/rlang_error>
Error in `filter()`:
! Can't transform a data frame with duplicate names.
---
Backtrace:
1. aa1 %>% filter(POS_strt == "20696")
3. dplyr:::filter.data.frame(., POS_strt == "20696")
Run `rlang::last_trace()` to see the full context.
在网上找了很久,终于解决了,总结一下。
问题解决
主要是因为数据框中的列名有重复,其实报错中也给出了:
! Can’t transform a data frame with duplicate names.
但是,我以为是rownames有重复,其实是colnames有重复,这里检查一下:
names(aa1) %>% duplicated() %>% table

可以看到,有一个有重复。
用make.names处理一下列名,重复的后缀加.1
nn = names(aa1)
names(aa1) = make.names(nn,unique = T)
names(aa1)

可以看到,图片中的红框部分,Chrom.1名称变了,这样处理后,就不会有重复了。测试一下:

搞定!
总结
tidyverse的数据框,不能有列名重复,否者filter之类的函数都不能使用。解决方案可以用base包中的make.names处理,进行重命名。
边栏推荐
- 华三交换机清空配置
- WireGuard简单配置
- 100 important knowledge points that SQL must master: join table
- Introduction to game theory
- LVGL 8.2 Simple Drop down list
- List介绍
- LVGL 8.2 Simple Colorwheel
- 我们公司使用 7 年的这套通用解决方案,打通了几十个系统,稳的一批!
- The two e-commerce bigwigs' lacy news screens represent the return of e-commerce to normal, which will be beneficial to the real economy
- 焕发青春的戴尔和苹果夹击,两大老牌PC企业极速衰败
猜你喜欢
随机推荐
什么是微信小程序,带你推开小程序的大门
OceanBase 安装 yum 源配置错误及解决办法
SQL必需掌握的100个重要知识点:使用表别名
语音识别-基础(一):简介【语音转文本】
100 important knowledge points that SQL must master: summary data
煥發青春的戴爾和蘋果夾擊,兩大老牌PC企業極速衰敗
How harmful are these "unreliable" experiences in the postgraduate entrance examination?
IDEA 又出新神器,一套代码适应多端!
博弈论入门
暑假学习记录
100 important knowledge points that SQL must master: using table aliases
Introduction to game theory
中国将强制统一充电接口,苹果如不低头,iPhone将被踢出中国市场
基于HAL库的按键(KEY)库函数
100 important knowledge points that SQL must master: using subquery
Algorithme leetcode 86. Liste des liens séparés
ArrayList and sequence table
200000 bonus pool! [Alibaba security × ICDM 2022] the risk commodity inspection competition on the large-scale e-commerce map is in hot registration
What is erdma as illustrated by Coptic cartoon?
[STL source code analysis] container (to be supplemented)








