[英]R: Remove “.” and 0 in year column in a data frame
I have a quite basic question, but I am not sure how to handle it in a smart way.我有一个非常基本的问题,但我不确定如何以聪明的方式处理它。
I have a column with quarterly year dates of the form yyyy.0q (y for year and q for quarter).我有一列包含 yyyy.0q 形式的季度年份日期(y 表示年份,q 表示季度)。 Example: 1990.01, 1990.02, 1990.03,...
示例:1990.01、1990.02、1990.03、...
I want to change the format to: 19901,19902,19903,... So, basically to remove the "."我想将格式更改为:19901,19902,19903,... 所以,基本上要删除“。” and the foregoing "0".
和前面的“0”。 The column is numeric.
该列是数字。
Is there a fast and convenient way to solve this problem?有没有快速方便的方法来解决这个问题?
library(tidyverse)
dat <- data.frame(x = c("1990.01", "1990.02", "1990.03"))
dat %>%
mutate(x2 = str_replace(x, "\\.0", ""))
which gives:这使:
x x2
1 1990.01 19901
2 1990.02 19902
3 1990.03 19903
We can use str_remove
我们可以使用
str_remove
library(dplyr)
library(stringr)
dat %>%
mutate(x2 = str_remove(x, "\\.0"))
dat <- data.frame(x = c("1990.01", "1990.02", "1990.03"))
There are already two answers, by deschen and akrun and one in comment ( mine ).已经有两个答案,由deschen和akrun 提供,一个在评论中( 我的)。 Here is a speed comparison run with package
microbenchmark
.这是使用 package
microbenchmark
运行的速度比较。
First, my solution.首先,我的解决方案。
x <- scan(what = character(), text = "1990.01 1990.02 1990.03")
sub("\\..", "", x)
#[1] "19901" "19902" "19903"
Now the test.现在测试。 I will create a bigger vector.
我将创建一个更大的向量。
library(microbenchmark)
x2 <- x
for(i in 1:log2(1e4/nchar(x))[1]) x2 <- c(x2, x2)
mb <- microbenchmark(
base_Rui = sub("\\.0", "", x2),
stringr_deschen = str_replace(x2, "\\.0", ""),
stringr_akrun = str_remove(x2, "\\.0")
)
print(mb, order = "median")
#Unit: milliseconds
# expr min lq mean median uq max neval cld
# base_Rui 2.060452 2.274474 2.531059 2.310165 2.410621 6.503303 100 a
# stringr_deschen 2.092459 4.181407 4.598719 4.265935 4.390778 11.885202 100 b
# stringr_akrun 3.754172 4.194410 4.624510 4.283582 4.499489 9.093461 100 b
With small vectors the difference is more impressive, try it on x
above.对于小的向量,差异更令人印象深刻,请在上面的
x
上尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.