R:从向量元素中删除向量元素

时间:2017-02-28 09:28:24

标签: r vector gsub

我认为标题有点令人困惑,但在这里我的问题: 我有2个向量,一个包含一些文本,另一个包含一些短语

class UserDepartmentRoleLink {

    @EmbeddedId
    private UserDepartmentRoleLinkId linkPk 
          = new UserDepartmentRoleLinkId(); 

    @Transient
    public User getUser() {
        return getLinkPk().getUser();
    }

    @Transient
    public User getDepartment() {
        return getLinkPk().getDepartment();
    }

    @Transient
    public User getRole() {
        return getLinkPk().getRole();
    }    
 }

@Embeddable
public class UserDepartmentRoleLinkId implements java.io.Serializable {

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "department_id")
    private Department department;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "role_id")
    private Role role;

现在我要删除文本中text <- c("this is some text","some elements should be removed", "i hope you can help me with this text element problem") pattern <- c("text", "some","be") 的所有元素,以及结果向量

pattern

我试过

text_result
[1] "this is"
[2] "elements should removed"
[3] "i hope you can help me with this element problem"

text_result <- sapply(pattern, function(x) gsub(x, text, replacement =""))

但在这两种情况下我都会收到一个带

的大矩阵
text_result <- sapply(text, function(y) sapply(pattern, function(x)gsub(x,y,replacement ="")))

提前感谢!

1 个答案:

答案 0 :(得分:0)

您可以尝试:

`%notin%` <- function(x,y) !(x %in% y)
 lapply(strsplit(text," "),function(x) paste(x[x %notin% pattern],collapse=" "))