如何转换字符串,如#34; Q12000"使用R?
中的Sub ReplaceAll_NL()
'ActiveCell.Select '<-- This line was changing the selection to be something
' different to what the user had selected.
Selection.Replace What:="NL", Replacement:="N", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
进入四分之一格式
我有一个季度矢量:
as.yearqtr
我尝试了以下代码:
x <- c("Q12000", "Q22000", "Q32000")
结果应该是&#34; 2000 Q1&#34; &#34; 2000 Q2&#34; &#34; 2000 Q3&#34;。相反,我得到所有library(zoo)
as.yearqtr(x, format = "Q%q%Y")
# [1] NA NA NA
。
答案 0 :(得分:2)
不幸的是,实现并不理解四分之一只能是一位数,所以它需要用非数字分隔。例如,在季度之后插入一个空格:
as.yearqtr(sub("(..)", "\\1 ", x), format = "Q%q %Y")
## [1] "2000 Q1" "2000 Q2" "2000 Q3"
或首先将其置于默认格式:
as.yearqtr(sub("Q(.)(....)", "\\2-\\1", x))
## [1] "2000 Q1" "2000 Q2" "2000 Q3"
注意:强>
x <- c("Q12000", "Q22000", "Q32000")