我有一个向量x,我想根据向量y中的值的顺序排序.两个向量的长度不同.
x <- c(2, 2, 3, 4, 1, 4, 4, 3, 3)
y <- c(4, 2, 1, 3)
Run Code Online (Sandbox Code Playgroud)
预期结果将是:
[1] 4 4 4 2 2 1 3 3 3
Run Code Online (Sandbox Code Playgroud) 如何在下面的例子中跳过写入文件"test.txt"的步骤,即将cat-result分配给一个对象,并仍然达到相同的最终结果?
我想我会包含完整的例子来说明问题的背景.
test <- c("V 1", "x", "1 2 3", "y", "3 5 8", "V 2", "x", "y", "V 3", "y", "7 2 1", "V 4", "x", "9 3 7", "y")
# Write selection to file
cat(test, "\n", file="test.txt")
test2 <- readLines("test.txt")
test3 <- strsplit(test2, "V ")[[1]][-1]
# Find results
x <- gsub("([0-9]) (?:x )?([0-9] [0-9] [0-9])?.*", "\\1 \\2 ", test3, perl = TRUE)
y <- gsub("([0-9]).* y ?([0-9] [0-9] [0-9])?.*", "\\1 \\2 ", test3, perl = TRUE)
# Eliminate …
Run Code Online (Sandbox Code Playgroud) 如何使用gsub超过9个反向引用?我希望下面例子中的输出是"e,g,i,j,o".
> test <- "abcdefghijklmnop"
> gsub("(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)", "\\5, \\7, \\9, \\10, \\15", test, perl = TRUE)
[1] "e, g, i, a0, a5"
Run Code Online (Sandbox Code Playgroud) 我想用来在文档中tikzDevice
包含带注释的ggplot2
图形Latex
.
tikzAnnotate
help有一个如何将它与基本图形一起使用的示例,但是如何将它与基于网格的绘图包一起使用ggplot2
?挑战似乎是tikz节点的定位.
playwith
包有一个功能convertToDevicePixels
(http://code.google.com/p/playwith/source/browse/trunk/R/gridwork.R)似乎与grconvertX/grconvertY类似,但我无法让它工作无论是.
将不胜感激任何有关如何进行的指示.
tikz使用基本图形注释示例
library(tikzDevice)
library(ggplot2)
options(tikzLatexPackages = c(getOption('tikzLatexPackages'),
"\\usetikzlibrary{shapes.arrows}"))
tikz(standAlone=TRUE)
print(plot(15:20, 5:10))
#print(qplot(15:20, 5:10))
x <- grconvertX(17,,'device')
y <- grconvertY(7,,'device')
#px <- playwith::convertToDevicePixels(17, 7)
#x <- px$x
#y <- px$y
tikzAnnotate(paste('\\node[single arrow,anchor=tip,draw,fill=green] at (',
x,',',y,') {Look over here!};'))
dev.off()
Run Code Online (Sandbox Code Playgroud)
由此:
> test <- data.frame(x = c("a","a","a"), y = c("b","b","c"), z = c(1,2,1))
> test
x y z
1 a b 1
2 a b 2
3 a c 1
Run Code Online (Sandbox Code Playgroud)
对此:
x b c
1 a 1 NA
2 a 2 NA
3 a NA 1
Run Code Online (Sandbox Code Playgroud)