r - Extracting words of a vector with names
问题描述
This is my vector:
m.v<- c("Aert~Derr", "Aert~Derr", "Aert~Derr", "Aert~Derr",
"Aert~Derr", "Aert~Derr", "Aert~Derr", "Aert~Derr", "Aert~Derr")
How can I create another vector with only the first name before the ~
?
So, how can I extract the first names?
解决方案
Here you have some other alternatives:
> gsub("~.*$", "", m.v)
[1] "Aert" "Aert" "Aert" "Aert" "Aert" "Aert" "Aert" "Aert" "Aert"
> library(stringr)
> str_extract(m.v, "^\\w+")
[1] "Aert" "Aert" "Aert" "Aert" "Aert" "Aert" "Aert" "Aert" "Aert"
推荐阅读
- python - 将 numpy 乘以数据帧,其中 numpy 中的索引号与数据帧值匹配
- ruby-on-rails - rails helper - 有条件地产生块或回退
- c++ - CMake:Visual Studio 2019 中 CXX 编译器的路径?
- c++ - 具有类型条件的 C++ 模板
- c++ - 从相反的顺序获取2个向量之间的公共值
- python - 不支持直通,GL是swiftshader,ANGLE是
- html - 检测 iframe 错误 ERR_BLOCKED_BY_RESPONSE
- pandas - Pandas DataFrame:一列中的值基于另一列中的先前值,第三列中的相同值
- r - 基于 session$user 预填充字段
- reactjs - Firebase 导入错误:“__spreadArray”未从“tslib”导出