我需要有关正则表达式的帮助才能执行以下操作。我有一个学习科目列表,例如:
subject <- c('x-010', 'x-011', 'x-012', 'x-013', 'x-014', 'x-015', 'x-016', 'x-017', 'x-018', 'x-019', 'x-020', 'x-021', 'x-022', 'x-023', 'x-024', 'x-025', 'x-026', 'x-027', 'x-028', 'x-029', 'x-030')
df <- data.frame(subject)
我想添加一列以按照主题的数量对主题进行分类,例如A组中的1-10,B组中的11-20,C组中的21-30,依此类推。我不知道如何使用正则表达式来做到这一点,只是从以下内容开始:
df <- data.frame(subject) %>%
mutate(case_when(group = str_detect(subject,
但需要了解如何描述这种模式。
答案 0 :(得分:2)
我们可以提取数字部分并使用library(tidyverse)
df %>%
group_by(grp = paste0("Group ", LETTERS[(as.numeric(str_extract(subject,
"[0-9]+"))-1) %/% 10 + 1]))
# A tibble: 21 x 2
# Groups: grp [3]
# subject grp
# <fct> <chr>
# 1 x-010 Group A
# 2 x-011 Group B
# 3 x-012 Group B
# 4 x-013 Group B
# 5 x-014 Group B
# 6 x-015 Group B
# 7 x-016 Group B
# 8 x-017 Group B
# 9 x-018 Group B
#10 x-019 Group B
# ... with 11 more rows
import React, { Component } from "react";
const homepg = "Hi! Welcome to my site.";
const aboutpg = "I am a React Web Developer.";
class App extends Component {
constructor(props) {
super(props);
this.state = { PageCon: "" };
this.homepg = this.homepg.bind(this);
this.aboutpg = this.aboutpg.bind(this);
}
homepg() {
this.setState({ PageCon: homepg });
}
aboutpg() {
this.setState({ PageCon: aboutpg });
}
render() {
const { PageCon } = this.state;
return (
<div className="App">
<nav>
<h1 onClick={this.homepg}>Home</h1>
<h1 onClick={this.aboutpg}>About</h1>
</nav>
<p>{PageCon}</p>{" "}
</div>
);
}
}
export default App;