如何在行而不是列上应用 dplyr 的 select(,starts_with()) ? [英] How can I apply dplyr's select(,starts_with()) on rows, not columns?

查看:19
本文介绍了如何在行而不是列上应用 dplyr 的 select(,starts_with()) ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正试图找出一种方法来选择在变量中具有特定值或特定字母的数据,尤其是使用与 starts_with() 类似的算法.

I'm now trying to figure out a way to select data having specific values in a variable, or specific letters, especially using similar algorithm that starts_with() does.

假设我有一个名为学校"的数据,如下所示:

Say I have a data named "school" as below:

Name Math English
James 80   90
Tom   91   91
Shaun 99   71
Jack  92   91

在这里, select(school, starts_with("M")) 只给我列数学".我想在行上应用它,例如命令'给我名称以J"字母开头的行',这反过来会给我一个两行的数据.

here, select(school, starts_with("M")) gives me the column "Math" only. I want to apply this on rows, such as to command 'give me the rows where name starts with "J" letter', which will in turn give me a data with two rows.

我尝试转置数据并成功实现了我想要的结果,但这并不是我真正想要的.

I tried transposing the data and succeeded at what I wanted to get, but it's not what I really want though.

我怎样才能完成这项工作?

How can I do this work?

推荐答案

我认为dplyr的filter和substring命令的结合是最有效的:

I believe that the combination of dplyr's filter and the substring command are the most efficient:

library(dplyr)
filtered_df <- school %>% dplyr::filter(substr(Name,1,1) == "J")

这篇关于如何在行而不是列上应用 dplyr 的 select(,starts_with()) ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆