r - 拆分数据框列——使用第二个大写字母作为分隔符
问题描述
我有一个这样的标签列表。
Label = c("ProjectCrash", "ProjectNoCrash", "TreatmentFed", "TreatmentPre",
"TreatmentStarve")
并且标签在数据框/小标题中
myTibble <- tibble(Label = Label)
myTibble
我想再创建两列,以第二个大写字母分隔。所以第一列将包含“项目”或“治疗”。
第二个将包含 Crash、NoCrash、Fed、Pre 或 Starve 之一。
解决方案
你也可以separate
从 tidyr使用
tidyr::separate(myTibble, Label,c("a","b"), "(?<=[a-z])(?=[A-Z])",
extra = "merge", remove = FALSE)
# A tibble: 5 x 3
Label a b
<chr> <chr> <chr>
1 ProjectCrash Project Crash
2 ProjectNoCrash Project NoCrash
3 TreatmentFed Treatment Fed
4 TreatmentPre Treatment Pre
5 TreatmentStarve Treatment Starve
在基础 R 中,您可以执行以下操作:
transform(myTibble, a=strcapture('([A-Z][a-z]+)(\\w+)',Label,
data.frame(a=character(), b=character())))
Label a.a a.b
1 ProjectCrash Project Crash
2 ProjectNoCrash Project NoCrash
3 TreatmentFed Treatment Fed
4 TreatmentPre Treatment Pre
5 TreatmentStarve Treatment Starve
推荐阅读
- django - Django 有时会从某种缓存中接收 ManyToManyField 查找结果
- python - 我们如何在 python 中使用 Coalesce 来处理使用 pandas 的多个数据帧
- python - 无法使用 Python 和 geckodriver 在 Raspbian 下运行 selenium-webdriver
- ruby-on-rails - 基于where谓词从rails API中删除的Curl命令?
- objective-c - 为什么 Xcode 允许创建重复的 Objective-C 类别?
- reactjs - 有条件渲染的两个兄弟 React 组件的生命周期方法是否有任何同步?
- tensorflow.js - 如何使用model.predict?
- c++ - 序列化包含字符串的类,通过套接字通过网络发送它?
- jquery - jQuery 隐藏论坛中的特定评论
- python - 引入高斯模糊后使用原始名称保存多张图像