删除R中字符串中的竖线

时间:2018-09-30 14:17:08

标签: r

我有这个数据集:

title <- c("Make Professional Maps with QGIS and Inkscape | Udemy", "Inkscape desde cero. Crea gráficos vectoriales con Inkscape | Udemy", 
"Logo Design in Inkscape - For Beginners & Beyond | Udemy", "Creating Seamless Tiles in Inkscape | Udemy", 
"Learn Inkscape : Design Logos and Game Arts | Udemy", "Inkscape 101 for Beginners - Design Vector Graphics | Udemy", 
"Design & Create Vector Graphics With Inkscape 2016 | Udemy", 
"Inkscape - Beginner to Pro | Udemy", "Créer un logo avec Inkscape | Udemy", 
"​​​Inkscape: Sıfırdan başlayarak vektörel çizim öğrenin​ | Udemy", 
"Creating 2D Textures in Inkscape | Udemy", "Inkscape fácil - Edición de gráficos vectoriales | Udemy", 
"Inkscape - Criando artes gráficas livremente | Udemy", "【完全版】Inkscape(インクスケープ)でプロ級販促物を作成できる実践講座 | Udemy", 
"Vector Art in Inkscape - Icon Design | Make Vector Graphics | Udemy", 
"Aprenda a criar arte vetorial para jogos 2d com o Inkscape! | Udemy", 
"Inkscape and Bootstrap 3 -> Responsive Web Design! | Udemy")

如您所见,它是一个包含17个标题的向量,所有标题均以| Udemy结尾。

如何删除| Udemy(竖线和Udemy词)?

我尝试了str_replacegrep,但没有成功。有想法吗?

3 个答案:

答案 0 :(得分:1)

使用sub的一个选项:

x <- "Make Professional Maps with QGIS and Inkscape | Udemy"
sub("\\| Udemy$", "", x)

这是说以文本| Udemy为目标,但仅当它出现在输入字符串的最末端时。

答案 1 :(得分:1)

tidyverse方法是使用stringr包(用于简单地删除包含udemy的文本)和/或tidyr包,用于基于“ |”将数据分为两列符号。

require(tidyverse)

dta <- data_frame(title = title)

dta %>% 
    mutate(new_title = stringr::str_replace(title, "\\| Udemy", "")) # double \\ to interpret | symbol literally

dta %>% separate(title, into = c("course", "provider"), sep = "\\|") 

两种方法都应该起作用。如果使用后者,则您现在想要的信息将在课程列中,但是您以后可能要参考的信息将在提供者列中。 (您可能也希望将str_trim应用于后者,以删除前导空格。)

(注意:如果对结果满意,请记住保存输出。)

答案 2 :(得分:0)

使用public partial class AlternativeToCellEndEdit : Form { public AlternativeToCellEndEdit() { InitializeComponent(); dataGridView1.CellValueChanged += DataGridView1_CellValueChanged; } // Try handling CellValueChanged instead of CellEndEdit private void DataGridView1_CellValueChanged(Object sender, DataGridViewCellEventArgs e) { // Our MyRecord class is smart and makes the calculation // any time we change Quantity or Amt. Here, we only need // to refresh the DataGridView to show the updated info. dataGridView1.Refresh(); } // Make a class to represent a line item in the DataGridView. // When the Quantity or Amt changes, it recalculates itself. class MyRecord { public string Description { get; set; } = "New Item"; int mQuantity = 1; public int Quantity { get { return mQuantity; } set { mQuantity = value; NetPrice = Quantity * Amt; } // Recalc } double mAmt = 0.00; public double Amt { get { return mAmt; } set { mAmt = value; NetPrice = Quantity * Amt; } // Recalc } public double NetPrice { get; private set; } // Makes this cell Read-Only in the DataGridView } // Tell the DataGridView that we want to display our custom class. BindingList<MyRecord> Items = new BindingList<MyRecord>(); protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); // Bind data to the view. // Now DataGridView does all the work for us. dataGridView1.DataSource = Items; // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // Make everything look nice in the way of formatting. DataGridViewColumn col; col = dataGridView1.Columns["Description"]; col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; col = dataGridView1.Columns["Quantity"]; col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; col = dataGridView1.Columns["Amt"]; col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; col.DefaultCellStyle.Format = "F2"; col = dataGridView1.Columns["NetPrice"]; col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; col.DefaultCellStyle.Format = "F2"; // Add the first default item Items.Add(new MyRecord()); } }

gsub

结果为:

gsub(" \\| Udemy", "", title)