如何使用r将特定数据从一列移动到另一列

时间:2021-12-01 05:16:50

Im having a problem trying to move data from my address column to my postal code column. For example:

我试图将数据从我的地址列移动到我的邮政编码列时遇到问题。例如:

如何使用r将特定数据从一列移动到另一列

On the second line im trying to take "Dublin 22" from the data.Address column and moving it to the data.Postal.Code column.

在第二行我试图从data.Address列中取“Dublin 22”并将其移动到data.Postal.Code列。

Im using R but i have no idea how to implement it.

我使用R但我不知道如何实现它。

Any suggestions?

1 个解决方案

#1


0  

Try this:

data.Postal.Code <- gsub("^.*, (.*)$", "\\1", data.Address)

Update:

If you want to move Dublin 22 to the postal code column whenever it appears in the address then you can try the following:

如果您想将Dublin 22移动到邮政编码列,只要它出现在地址中,那么您可以尝试以下操作:

data.Postal.Code[grepl("^.* Dublin 22$", data.Address)] <- "Dublin 22"

Here is a demo of the regex used:

这是使用的正则表达式的演示:

Regex101

#1


0  

Try this:

data.Postal.Code <- gsub("^.*, (.*)$", "\\1", data.Address)

Update:

If you want to move Dublin 22 to the postal code column whenever it appears in the address then you can try the following:

如果您想将Dublin 22移动到邮政编码列,只要它出现在地址中,那么您可以尝试以下操作:

data.Postal.Code[grepl("^.* Dublin 22$", data.Address)] <- "Dublin 22"

Here is a demo of the regex used:

这是使用的正则表达式的演示:

Regex101