在另一列

时间:2018-01-31 22:00:43

标签: r

我有一个像这样的数据框:

 before<- data.frame( Var1= 
  c("174_1","174_1","174_2","174_3","175_1","175_1"))

我想在下划线之前添加另一列Var2,其中包含Var1中表达式的一部分。新列将如下所示:

after<- data.frame( Var1= 
  c("174_1","174_1","174_2","174_3","175_1","175_1"), Var2= 
  c("174","174","174","174","175","175"))

我相信像grepl()这样的函数可能对此有用,但是,我不知道如何在grepl(“_”)之前指定保留部分内容。

3 个答案:

答案 0 :(得分:1)

TaggableType

这会占用所有内容的子字符串直到下划线

答案 1 :(得分:1)

使用cleartool find . -exec "cleartool describe -fmt \"%n %l\n\" \"%CLEARCASE_PN%\""

class ShoppingCart : public Customer
{
    private:
    Item* cart[MAX_SIZE] = {NULL};  //problem  may occur
    int size;

    public:
    void addItem(char* type, char* title, char* description, double price);
    void showCart();
};

class Customer
{
    private:
    int ID, items;
    char* firstName, *lastName;
    ShoppingCart* Cart;

    public:
    Customer(char* userFirst, char* userLast, int userID, ShoppingCart* cart)
    {
        this->ID = userID;
        this->firstName = userFirst;
        this->lastName = userLast;
        this->Cart = cart;
    }
    friend void ShoppingCart::addItem(char* type, char* title, char* description, double price);
};

答案 2 :(得分:1)

before <- data.frame(Var1= c("174_1","174_1","174_2","174_3","175_1","175_1"))

after <- data.frame(Var1 = before$Var1,Var2 = unlist(lapply(strsplit(as.character(before$Var1), '_'), `[[`,1)))