R按变量中的特定值排列行
我想在我的tibble中排列这些行,以便其中包含“Gas”的值进入tibble的底部 以下是我的数据:R按变量中的特定值排列行,r,dplyr,R,Dplyr,我想在我的tibble中排列这些行,以便其中包含“Gas”的值进入tibble的底部 以下是我的数据: library(dplyr) df1 <- tibble( col1 = c("ZBottom Gas","Almost Bottom Gas","Top","Bottom Gas", "Top"), col2 = c(5, 7, 4, 8,6)) 库(dplyr) df1我们可以使用stru detect来检测“Gas”的存在,并将其用于arrange library(dp
library(dplyr)
df1 <- tibble(
col1 = c("ZBottom Gas","Almost Bottom Gas","Top","Bottom Gas", "Top"),
col2 = c(5, 7, 4, 8,6))
库(dplyr)
df1我们可以使用stru detect
来检测“Gas”
的存在,并将其用于arrange
library(dplyr)
library(stringr)
df1 %>% arrange(str_detect(col1, 'Gas'))
# col1 col2
# <chr> <dbl>
#1 Top 4
#2 Top 6
#3 ZBottom Gas 5
#4 Almost Bottom Gas 7
#5 Bottom Gas 8
regexpr()
df1[order(regexpr("Gas", df1$col1)), ]
# # A tibble: 5 x 2
# col1 col2
# <chr> <dbl>
# 1 Top 4
# 2 Top 6
# 3 Bottom Gas 8
# 4 ZBottom Gas 5
# 5 Almost Bottom Gas 7
df1[订单(regexpr(“Gas”,df1$col1)),]
##tibble:5 x 2
#col1 col2
#
#1前4名
#2前6名
#3底部气体8
#4兹伯顿煤气公司5
#5几乎是底部气体7
太好了!从没听说过grepl,不过它会很有用的
library(dplyr)
library(stringr)
df1 %>% arrange(str_detect(col1, 'Gas'))
# col1 col2
# <chr> <dbl>
#1 Top 4
#2 Top 6
#3 ZBottom Gas 5
#4 Almost Bottom Gas 7
#5 Bottom Gas 8
df1[order(grepl('Gas', df1$col1)), ]
df1[order(regexpr("Gas", df1$col1)), ]
# # A tibble: 5 x 2
# col1 col2
# <chr> <dbl>
# 1 Top 4
# 2 Top 6
# 3 Bottom Gas 8
# 4 ZBottom Gas 5
# 5 Almost Bottom Gas 7