遗憾的是,您经常会看到有关SO的问题,这些问题以不可复制的格式呈现数据; 往往只是复制的结果print()...
set.seed(1)
x <- sample(LETTERS, 40, replace = T)
y <- rnorm(20)
Run Code Online (Sandbox Code Playgroud)
......比如这样:
x
[1] "G" "J" "O" "X" "F" "X" "Y" "R" "Q" "B" "F" "E" "R" "J" "U" "M" "S"
[18] "Z" "J" "U" "Y" "F" "Q" "D" "G" "K" "A" "J" "W" "I" "M" "P" "M" "E"
[35] "V" "R" "U" "C" "S" "K"
Run Code Online (Sandbox Code Playgroud)
... 或这个:
y
[1] 0.91897737 0.78213630 0.07456498 -1.98935170 0.61982575
[6] -0.05612874 -0.15579551 -1.47075238 -0.47815006 0.41794156
[11] 1.35867955 -0.10278773 0.38767161 -0.05380504 -1.37705956
[16] -0.41499456 -0.39428995 -0.05931340 1.10002537 0.76317575
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望能够将上面的块中的文本复制到我的剪贴板,并调用一些函数foo(),以便all.equal(foo(), x)对离散数据类型和all(near(foo(), y))浮点数(给定打印精度).
是否有一种简单的方法(大致)从复制的结果中重建一个简单的向量print()?
y_printed <- capture.output(y)
Run Code Online (Sandbox Code Playgroud)
我用scan这个问题。
你能用下面的代码创建一个函数吗?
y <-
'[1] 0.91897737 0.78213630 0.07456498 -1.98935170 0.61982575
[6] -0.05612874 -0.15579551 -1.47075238 -0.47815006 0.41794156
[11] 1.35867955 -0.10278773 0.38767161 -0.05380504 -1.37705956
[16] -0.41499456 -0.39428995 -0.05931340 1.10002537 0.76317575'
y <- scan(what = character(), text = y)
y <- sub("^\\s*\\[\\d+\\]", "", y)
y <- as.numeric(y[y != ""])
Run Code Online (Sandbox Code Playgroud)
根据@Moody_Mudskipper 评论中的建议,
模式可以更新为“^\s*\[\d+\]”以支持OP的示例(以空格开头)。
一个函数可以是
recreateVector <- function(X, numeric = TRUE, quiet = FALSE){
X <- scan(what = character(), text = X, quiet = quiet)
X <- sub("^\\s*\\[\\d+\\]", "", X)
X <- X[X != ""]
if(numeric) X <- as.numeric(X)
X
}
recreateVector(y) # Use the original y
#Read 24 items
# [1] 0.91897737 0.78213630 0.07456498 -1.98935170 0.61982575
# [6] -0.05612874 -0.15579551 -1.47075238 -0.47815006 0.41794156
#[11] 1.35867955 -0.10278773 0.38767161 -0.05380504 -1.37705956
#[16] -0.41499456 -0.39428995 -0.05931340 1.10002537 0.76317575
Run Code Online (Sandbox Code Playgroud)
对于字符向量,设置参数numeric = FALSE,默认值为TRUE。
x <-
'[1] "G" "J" "O" "X" "F" "X" "Y" "R" "Q" "B" "F" "E" "R" "J" "U" "M" "S"
[18] "Z" "J" "U" "Y" "F" "Q" "D" "G" "K" "A" "J" "W" "I" "M" "P" "M" "E"
[35] "V" "R" "U" "C" "S" "K"'
recreateVector(x, numeric = FALSE)
#Read 43 items
# [1] "G" "J" "O" "X" "F" "X" "Y" "R" "Q" "B" "F" "E" "R" "J" "U"
#[16] "M" "S" "Z" "J" "U" "Y" "F" "Q" "D" "G" "K" "A" "J" "W" "I"
#[31] "M" "P" "M" "E" "V" "R" "U" "C" "S" "K"
Run Code Online (Sandbox Code Playgroud)
注意论证quiet。我已将默认值设置为FALSE,就像在 的定义中一样scan,因为我更喜欢查看是否实际读入了任何内容。