使用grep函数根据另一列中的匹配字符串创建新列

时间:2019-10-02 01:27:38

标签: r regex grep

我正在尝试使用grep函数基于匹配的字符串(不匹配= 0,匹配= 1)创建新列,但未获得预期结果

public void OracleConnTimeoutTest()
{
    string oralceConnStr = ConfigurationManager.ConnectionStrings["<CONNECTION STRING>"].ConnectionString;
    OracleConnection oraConnection = new OracleConnection(oralceConnStr);

    OracleCommand oraCmd = new OracleCommand();
    oraCmd.Connection = oraConnection;

    // Stored procedure execution
    oraCmd.CommandText = "TESTDBCONNSQL";
    oraCmd.CommandType = CommandType.StoredProcedure;

    try
    {
        oraConnection.Open();

        // WHEN I SET THE COMMANDTIMEOUT = 60 sec
        // oraCmd.CommandTimeout = 60;
        int result = oraCmd.ExecuteNonQuery();

        // Stored procedure return success or failure
        var spResult = oraCmd.Parameters["Output_Value"].Value;
    }
    catch (Exception ex)
    {
        System.Console.WriteLine("Exception: {0}", ex.ToString());
    }
}

1 个答案:

答案 0 :(得分:3)

grep / grepl被矢量化。我们可以在带有模式的列上直接使用它们,只需将其包装在as.integer中即可分别将逻辑值TRUE / FALSE转换为1/0

data$col2 <- as.integer(grepl("[0-9]\\.[0-9][0-9]|name|name2|wh??|[0-9]|\\?", data$col1))
data
#           col1 col2
#1          name    1
#2      no_match    0
#3           123    1
#4          0.19    1
#5        rand?m    1
#6 also_no_match    0