[英]Conditional changes of values in a column R
I'm trying to change the values in my Latitude and Longitude columns. 我正在尝试更改“纬度”和“经度”列中的值。 Instead of South (in latitude), I want to drop the S and make the number negative.
我想放南而不是南(纬度),并使数字为负。 If it is North, I just want to drop the N. I would like to do the same thing with Longitude, and drop the letters.
如果是北,我只想放N。我想对经度做同样的事情,然后放字母。 I want West to be negative and East be positive.
我希望西方是消极的,东方是积极的。
Here is a snip of my data frame 这是我的数据框的一部分
Please let me know how I can accomplish this! 请让我知道我该如何完成!
For a data frame defined as df
对于定义为
df
的数据帧
# build a sample data frame with two columns, lat and long
df <- data.frame(Lat = c("1.2N", "1.2S", "35.5N", "33.4S"),
Long = c("113.8W", "113.5W", "43.2E", "55.4E"))
# use gsub with signature gsub(PATTERN, REPLACEMENT, X)
# where you use a regex for the pattern and replacement
# and X is your target.
df$Lat <- gsub("(\\d*\\.\\d*)N","\\1", df$Lat, perl = TRUE)
df$Lat <- gsub("(\\d*\\.\\d*)S","-\\1", df$Lat, perl = TRUE)
df$Long <- gsub("(\\d*\\.\\d*)W","\\1", df$Long, perl = TRUE)
df$Long <- gsub("(\\d*\\.\\d*)E","-\\1", df$Long, perl = TRUE)
So now the only work left is to work out the regex, which isn't anything special to R, save that what you would use as \\w
anywhere else, in R you need to turn into \\\\w
, etc. 所以现在剩下的唯一工作就是计算正则表达式,这对R而言并没有什么特别之处,除了在其他任何地方用
\\w
,在R中您需要将\\\\w
等。
So, we want to match digits, so a single regex digit is (in R) \\\\d
, so we want to capture all of them before and after our decimal, which means 因此,我们想匹配数字,因此一个正则表达式数字是(R中的)
\\\\d
,因此我们想捕获十进制之前和之后的所有数字,这意味着
\\d*.\\d*
but that .
但是
.
is special in regex, so let's escape it for 正则表达式中的特殊字符,因此让我们对其进行转义
\\d*\\.\\d*
Now let's think of the N
, and we want to remove that in the replacement, so we need to group the digits so that we keep them in our match. 现在让我们想到
N
,我们想在替换中将其删除,因此我们需要对数字进行分组以使它们保持匹配。 We do this with parentheses. 我们用括号来做到这一点。 So, one group and the letter gives us
所以,一群人和这封信给我们
(\\d*\\.\\d*)N
we can refer to the captured group in our replacement with \\\\1
. 我们可以用
\\\\1
来指代我们捕获的捕获组。 So our replacement regex is simply 所以我们的替换正则表达式很简单
\\1 which really means \\d*\\.\\d*
and similarly for the S, where we add a -
to the front of each match with 和S类似,我们在每个匹配项的前面添加
-
-\\1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.