如何使用R中的gsub()函数替换“+”

时间:2017-05-14 15:53:16

标签: r regex dataframe gsub

我正在尝试删除数据框的一个字符串元素中存在的“+”字符。但我无法找到解决方法。

以下是数据框。

using ISTE.Models;
using ISTE.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace ISTE.Views
{
    /// <summary>
    /// Interaction logic for CoursesView.xaml
    /// </summary>
    public partial class CoursesView : UserControl
    {
        CoursesVM vm;
        public CoursesView()
        {
            InitializeComponent();
            vm = new CoursesVM();
            this.DataContext = vm;

            vm.GetData();
        }
        bool flag=false;
        bool flag1=false;
        private void UnderGraduate_Click(object sender1, RoutedEventArgs e1)
        {
            if (flag1 == false)
            {
                foreach (Undergraduate grad in vm.ItemData.undergraduate)
                {
                    var textb = new Button();

                    undergradpanel.Children.Add(textb);
                    textb.Content = grad.degreeName;
                    textb.Click += new RoutedEventHandler(ugdegreeEventHandler);
                    Console.WriteLine("graddegrees \t" + grad.degreeName);
                }
            }
            flag1 = true;
        }
        void ugdegreeEventHandler(object sender2, RoutedEventArgs e2)
        {
            // Console.WriteLine("hi select");
            var b = sender2 as Button;
            //  var textb2 = new TextBlock();
            //  textb2.Text

                 myBorder1.Background = Brushes.SkyBlue;
                 myBorder1.BorderBrush = Brushes.Blue;
                 myBorder1.BorderThickness = new Thickness(2);
                 myBorder1.CornerRadius = new CornerRadius(8);
                 myBorder1.Padding = new Thickness(6);


            var degreeselected = b.Content.ToString();

            if (undergradpaneldesc.Children.Count < 1)
            {


                var textb2 = new TextBlock();
                            textb2.Name = "textblock";

                undergradpaneldesc.Children.Add(textb2);

                foreach (Undergraduate grad in vm.ItemData.undergraduate)
                {
                    if (grad.degreeName == degreeselected)
                    {
                        textb2.TextDecorations = TextDecorations.Underline;
                        textb2.Text = grad.description+"\n\n";

                        textb2.TextWrapping = TextWrapping.Wrap;
                        return;
                    }
                }


            }
            else
            {

                foreach (Undergraduate grad in vm.ItemData.undergraduate)
                {
                    if (grad.degreeName == degreeselected)
                    {

                        foreach (TextBlock ch in undergradpaneldesc.Children)
                        {
                            ch.Text = "";
                            ch.TextDecorations = TextDecorations.Underline;
                            ch.Text = grad.description+"\n\n";
                            ch.TextWrapping = TextWrapping.Wrap;
                            return;
                        }
                    }
                }
            }



        }
        private void Graduate_Click(object sender, RoutedEventArgs e)
        {

            if (flag == false)
            {
                foreach (Graduate grad in vm.ItemData.graduate)
                {


                        var textb = new Button();
                        gradpanel.Children.Add(textb);
                        textb.Content = grad.degreeName;
                        textb.IsDefault = true;
                        textb.Click += new RoutedEventHandler(degreeEventHandler);

                        Console.WriteLine("graddegrees \t" + grad.degreeName);

                }
            }
            flag = true;
        }

        void degreeEventHandler(object sender, RoutedEventArgs e)
        {
            // Console.WriteLine("hi select");
            var b = sender as Button;
            //  var textb2 = new TextBlock();
            //  textb2.Text

            var degreeselected = b.Content.ToString();

                myBorder2.Background = Brushes.SkyBlue;
                myBorder2.BorderBrush = Brushes.Black;
                myBorder2.BorderThickness = new Thickness(2);

                myBorder2.CornerRadius = new CornerRadius(8);
                myBorder2.Padding = new Thickness(6);


            if (gradpaneldesc.Children.Count<1)
            {


                var textb1 = new TextBlock();
                textb1.Name = "textblock1";

                gradpaneldesc.Children.Add(textb1);

                foreach (Graduate grad in vm.ItemData.graduate)
                {
                    if (grad.degreeName == degreeselected)
                    {
                        if (grad.degreeName != "graduate advanced certificates")
                        {
                            textb1.Text = grad.description;
                            textb1.TextDecorations = TextDecorations.Underline;
                            foreach (var conc in grad.concentrations)
                            {
                                textb1.Text += conc + "\n\n";
                            }

                            textb1.TextWrapping = TextWrapping.Wrap;
                            return;
                        }
                        else
                        {

                            textb1.TextDecorations = TextDecorations.Underline;
                            foreach (var conc in grad.availableCertificates)
                            {
                                textb1.Text += conc + "\n\n";
                            }

                            textb1.TextWrapping = TextWrapping.Wrap;
                            return;
                        }
                    }
                }


            }
            else
            {

                foreach (Graduate grad in vm.ItemData.graduate)
                {
                    if (grad.degreeName == degreeselected)
                    {
                        if (grad.degreeName != "graduate advanced certificates")
                        {
                            foreach (TextBlock ch in gradpaneldesc.Children)
                            {
                                ch.Text = "";
                                ch.Text = grad.description + "\n\n";
                                ch.TextDecorations = TextDecorations.Underline;
                                foreach (var conc in grad.concentrations)
                                {
                                    ch.Text += conc + "\n";
                                }

                                ch.TextWrapping = TextWrapping.Wrap;
                                return;
                            }
                        }
                        else
                        {
                            foreach (TextBlock ch in gradpaneldesc.Children)
                            {
                                ch.Text = "";

                                ch.TextDecorations = TextDecorations.Underline;
                                foreach (var conc in grad.availableCertificates)
                                {
                                    ch.Text += conc + "\n";
                                }

                                ch.TextWrapping = TextWrapping.Wrap;
                                return;
                            }
                        }
                    }
                }
            }



        }

        private void Certifications_Click(object sender, RoutedEventArgs e)
        {
            var textb3 = new TextBlock();
            textb3.Name = "textblock3";

            certificatedesc.Children.Add(textb3);

            textb3.TextDecorations = TextDecorations.Underline;
            myBorder3.Background = Brushes.SkyBlue;
            myBorder3.BorderBrush = Brushes.Black;
            myBorder3.BorderThickness = new Thickness(2);

            myBorder3.CornerRadius = new CornerRadius(8);
            myBorder3.Padding = new Thickness(6);
            foreach (Graduate grad in vm.ItemData.graduate)
            {

                if (grad.degreeName == "graduate advanced certificates")
                {
                    textb3.Text = grad.description;
                    textb3.TextDecorations = TextDecorations.Underline;
                    foreach (var conc in grad.availableCertificates)
                    {
                        textb3.Text += conc + "\n\n";
                    }

                    textb3.TextWrapping = TextWrapping.Wrap;
                    return;
                }

            }
        }
    }
}

我尝试了两种方法,但都没有达到预期效果:

WAY1

txtdf <- structure(list(ID = 1:9, Var1 = structure(c(1L, 1L, 1L, 1L, 4L, 
            5L, 5L, 2L, 3L), .Label = c("government", "parliament", "parliment", 
            "poli+tician", "politician"), class = "factor")), .Names = c("ID", 
            "Var1"), class = "data.frame", row.names = c(NA, -9L))
#  ID   Var1
#  1    government
#  2    government
#  3    government
#  4    government
#  5    poli+tician
#  6    politician
#  7    politician
#  8    parliament
#  9    parliment

我不明白这里有什么问题。我希望'+'字符单独替换为第5个元素没有值,但所有元素都按上述方式编辑。

Way2

txtdf <- gsub("[:punct:]","", txtdf)
# [1] "goverme" "goverme" "goverme" "goverme" "oli+iia" "oliiia"  "oliiia" 
# [8] "arliame" "arlime" 

这里根本没有变化。我想我试过的是,我试图用双斜线逃避+字符。

2 个答案:

答案 0 :(得分:1)

你需要逃避你的加号,“+”在正则表达式方面具有特殊意义(它是量词),因此不能被视为标点符号,来自文档:?regex < / p>

  

“+”前面的项目将匹配一次或多次。

为了匹配这些特殊字符,你需要逃避它们,以便它们的含义可以从字面上理解,因此它们的特殊含义不会被翻译。在R中你需要两个反斜杠(\)才能逃脱。所以在你的情况下,这将是:

gsub("\\+","",df$job)

通过删除数据中的所有加号,

将在上面运行,从而为您提供所需的结果

所以假设你的df是:

df <- data.frame(job = c("government", "poli+tician","politician", "parliament"))

然后你的输出将是:

> gsub("\\+","",df$job)
[1] "government" "politician" "politician"
[4] "parliament"

答案 1 :(得分:0)

只需将其替换为fixed = TRUE(无需使用正则表达式),但您必须为每个&#34;列替换#34;通过指定列名称来指定data.frame:

txtdf <- data.frame(job = c("government", "poli+tician", "parliament"))
txtdf

给出

          job
1  government
2 poli+tician
3  parliament

现在替换&#34; +&#34;:

txtdf$job <- gsub("+", "", txtdf$job, fixed = TRUE)
txtdf

结果是:

         job
1 government
2 politician
3 parliament