将数据框中的字符向量与另一个字符向量匹配并修剪字符 [英] Match character vector in a dataframe with another character vector and trim character

查看:43
本文介绍了将数据框中的字符向量与另一个字符向量匹配并修剪字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个数据框和一个向量.

Here is a dataframe and a vector.

df1  <-  tibble(var1 = c("abcd", "efgh", "ijkl", "qrst"))

vec <-  c("abcd", "mnop", "ijkl")

现在,对于var1中与vec中的值匹配的所有值,在var1中仅保留前3个字符,以便所需的解决方案是:

Now, for all the values in var1 that matches with the values in vec, keep only first 3 characters in var1 such that the desired solution is:

df2 <- tibble(var1 = c("abc", "efgh", "ijk", "qrst"))

由于"abcd"匹配,因此在df2中仅保留3个字符,即"abc",但在vec中不存在"efgh",因此在df2中将其保留为即"efgh".

Since, "abcd" matches, we keep only 3 characters i.e. "abc" in df2, but "efgh" doesn't exist in vec, so we keep it as is i.e "efgh" in df2.

如何使用dplyr和/或stringr完成此操作?

How can I use dplyr and/or stringr to accomplish this?

推荐答案

您可以只使用%in%来检查字符串是否在向量中,并使用 substr 修剪矢量:

You can just use %in% to check whether the strings are in the vector, and substr to trim the vector:

df1 %>% 
    mutate(var1 = ifelse(var1 %in% vec, substr(var1, 1, 3), var1))

# A tibble: 4 x 1
#  var1 
#  <chr>
#1 abc  
#2 efgh 
#3 ijk  
#4 qrst

这篇关于将数据框中的字符向量与另一个字符向量匹配并修剪字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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