当前位置:网站首页>R语言dplyr中的Select函数变量列名
R语言dplyr中的Select函数变量列名
2022-07-04 03:52:00 【育种数据分析之放飞自我】
背景介绍:
R语言中,数据框列名是规范的,这里想提取Y2:Y11, Q1:Q5这样的列名,又不想写完整列名,
在外面生成一个变量名包含上面的内容,比如:
n1 = str_c("Y",2:11)
n1
n2 = str_c("Q",1:5)
n2
然后直接根据n1,n2的内容去提取。
问题解决
library(dplyr)
dd %>% select(n1,n2) %>% head

有一个前缀的bug,如果数据框中有n1和n2的列名,上面的数据就变为了:
dd$n1 = rnorm(44)
dd$n2 = rnorm(44)
library(dplyr)
dd %>% select(n1,n2) %>% head

显然,这不是我们想要的。
更好的解决方案
用
dplyr中的all_of函数,使用变量的内容,而不是变量的原始名称:
dd %>% select(all_of(n1),all_of(n2)) %>% head

这才是一个比较妥帖的方案。
问题解决!
边栏推荐
- (指针)编写函数void fun(int x,int *pp,int *n)
- JS实现文字滚动 跑马灯效果
- Tcp- simple understanding of three handshakes and four waves
- The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
- One click compilation and deployment of MySQL
- 量子力学习题
- Storage of MySQL database
- Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
- Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
- RHCSA 03 - 文件的基础权限
猜你喜欢

Rhcsa-- day one

软件测试是干什么的 发现缺陷错误,提高软件的质量

深度优先搜索简要讲解(附带基础题)

2020 Bioinformatics | TransformerCPI

【CSRF-01】跨站请求伪造漏洞基础原理及攻防

ctf-pikachu-XSS

There is a problem that the package cannot be parsed in the like project

DP83848+网线热拔插

dried food! Generation of rare samples based on GaN

Penetration practice - sqlserver empowerment
随机推荐
Tcpclientdemo for TCP protocol interaction
JDBC 进阶
【CSRF-01】跨站请求伪造漏洞基础原理及攻防
Flink学习8:数据的一致性
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
思考的小记录
Evolution of MySQL database architecture
Two commonly used graphics can easily realize data display
02 specific implementation of LS command
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
Redis:哈希hash类型数据操作命令
毕业总结
一位毕业生的自我分享
C语言单向链表练习
Huawei cloud Kunpeng engineer training (Guangxi University)
Small record of thinking
[Yugong series] go teaching course 002 go language environment installation in July 2022
(pointeur) Écrivez - vous une fonction qui compare la taille de la chaîne et fonctionne comme strcmp.
RHCSA 08 - automount配置
VIM mapping command