R在字符串中查找和替换

时间:2018-06-14 18:46:17

标签: r

我有一个单列的文本文件,我试图划分,但我无法锻炼最好的方法。

该列具有不同的日期格式,后跟文本,首先我尝试将所有日期设置为一种格式ddmmyyyy然后拆分日期。 3行数据的示例。

01/01/17 The address is...
17-05-2018 Please pay...
10 Jun 2018. Payee Name...

预期

01/01/2017|The address is...
17/05/2018|Please pay...
10/06/2018|Payee Name...

我在R中有一个名为REPLACEVALUES的数据框,其中包含2列OldTextNewText。我加载的csv文件名为Data

我尝试使用gsub,但我收到错误,这是我尝试过的代码。

DataCleaned<-gsub(REPLACEVALUES$OldText, REPLACEVALUES$NewText, Data)
Warning message argument 'Pattern' has length >1 and only first element will be used

我也尝试过使用下面的功能。 NewData<- Data NewData[]<-REPLACEVALUES$NewText[match(unlist(Data), REPLACEVALUES$OldText)]

1 个答案:

答案 0 :(得分:0)

这应该让你开始。这是寻找4个数字(基本上是年份),因此它可能不适用于存储在格式&#34; mm / dd / yy&#34;中的内容。你需要给它一些东西才能找到合适的分离。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import de.javagl.viewer.Painter;
import de.javagl.viewer.Viewer;

public class ScaledPaintingViewer
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(() -> createAndShowGUI());
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame("Viewer");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().setLayout(new BorderLayout());

        f.getContentPane().add(
            new JLabel("<html>"
                + "Right mouse drags: Translate<br> "
                + "Left mouse drags: Rotate<br>"
                + "Mouse wheel: Zoom uniformly<br>"
                + "&nbsp;&nbsp;&nbsp;&nbsp; +shift: zoom along x<br>"
                + "&nbsp;&nbsp;&nbsp;&nbsp; +ctrl: zoom along y<br>"
                + "</html>"),
            BorderLayout.NORTH);

        Viewer viewer = new Viewer();
        ScaledPaintingPainter painter = new ScaledPaintingPainter();
        viewer.addPainter(painter);
        viewer.setDisplayedWorldArea(painter.computeBounds());
        f.getContentPane().add(viewer, BorderLayout.CENTER);

        f.setSize(800,800);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

class ScaledPaintingPainter implements Painter
{
    double[][] subjPoints =
    {
        { 38.81904602050781, -71.00624084472656 },
        { 38.81904602050781, -70.29379272460938 },
        { 37.95466232299805, -70.35797882080078 },
        { 37.9495735168457, -71.03191375732422 } 
    };

    double[][] clipPoints =
    {
        { 38.62575820040764, -70.84753473092672 },
        { 38.418853759765625, -71.02689361572266 },
        { 38.21194931912361, -71.2057395294625 },
        { 37.931301169971185, -70.67791159484983 },
        { 38.1382056106132, -70.49975311140243 },
        { 38.34511005125521, -70.32108875708619 } 
    };

    @Override
    public void paint(
        Graphics2D g, AffineTransform worldToScreen, double w, double h)
    {
        // Convert the arrays into shapes
        Shape sShape = convertToShape(subjPoints);
        Shape cShape = convertToShape(clipPoints);

        g.setColor(Color.BLUE);
        g.draw(worldToScreen.createTransformedShape(sShape));
        g.setColor(Color.RED);
        g.draw(worldToScreen.createTransformedShape(cShape));
    }

    Rectangle2D computeBounds()
    {
        // Convert the arrays into shapes
        Shape sShape = convertToShape(subjPoints);
        Shape cShape = convertToShape(clipPoints);

        // Compute the bounding boxes of the shapes
        Rectangle2D sBounds = sShape.getBounds2D();
        Rectangle2D cBounds = cShape.getBounds2D();
        Rectangle2D bounds = new Rectangle2D.Double();
        Rectangle2D.union(sBounds, cBounds, bounds);

        return bounds;
    }

    private static Shape convertToShape(double array[][])
    {
        Path2D path = new Path2D.Double();
        for (int i=0; i<array.length; i++)
        {
            double a[] = array[i];
            double x = a[0];
            double y = a[1];
            if (i == 0)
            {
                path.moveTo(x, y);
            }
            else
            {
                path.lineTo(x, y);
            }
        }
        path.closePath();
        return path;
    }

}