当前位置:网站首页>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

这才是一个比较妥帖的方案。
问题解决!
边栏推荐
- Interpretation of leveldb source code skiplist
- Katalon framework tests web (XXI) to obtain element attribute assertions
- 图解网络:什么是热备份路由器协议HSRP?
- 北漂程序员,月薪20K,一年攒15W,正常吗?
- Pytest multi process / multi thread execution test case
- Smart subway | cloud computing injects wisdom into urban subway transportation
- 【华为云IoT】读书笔记之《万物互联:物联网核心技术与安全》第3章(上)
- Huawei cloud Kunpeng engineer training (Guangxi University)
- ctf-pikachu-CSRF
- 拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
猜你喜欢

02 ls 命令的具体实现

PostgreSQL users cannot create table configurations by themselves

2020 Bioinformatics | TransformerCPI

Msgraphmailbag - search only driveitems of file types

Idea modify body color

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

Confession code collection, who says program apes don't understand romance

Introduction to asynchronous task capability of function calculation - task trigger de duplication

Perf simple process for multithreaded profile

透过JVM-SANDBOX源码,了解字节码增强技术原理
随机推荐
【微服务|openfeign】使用openfeign远程调用文件上传接口
ctf-pikachu-XSS
Flink学习7:应用程序结构
Leetcode skimming: binary tree 07 (maximum depth of binary tree)
线程常用的方法
Mitsubishi M70 macro variable reading Mitsubishi M80 public variable acquisition Mitsubishi CNC variable reading acquisition Mitsubishi CNC remote tool compensation Mitsubishi machine tool online tool
[Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (I)
Programmers' telecommuting is mixed | community essay solicitation
2020 Bioinformatics | TransformerCPI
ROS2中CMake编译选项的设置
dried food! Generation of rare samples based on GaN
vim映射命令
The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
Configuration and hot update of nocturnal simulator in hbuildx
Katalon framework tests web (XXI) to obtain element attribute assertions
pytest多进程/多线程执行测试用例
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
(pointer) write a function to compare the size of strings by yourself, which is similar to StrCmp.
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure