我经常使用具有"R友好"/"程序员友好"列名的数据框,通常没有空格和/或缩写(懒惰以在进行分析时键入全名).例如:
ir <- data.frame(
sp=iris$Species,
sep.len=iris$Sepal.Length,
sep.wid=iris$Sepal.Width,
pet.len=iris$Petal.Length,
pet.wid=iris$Petal.Width
)
Run Code Online (Sandbox Code Playgroud)
当我用ggplot绘制这些时,我经常想用"用户友好"的列名替换标签,例如
p <- ggplot(ir, aes(x=sep.len, y=sep.wid, col=sp)) + geom_point() +
xlab("sepal length") + ylab("sepal width") +
scale_color_discrete("species")
Run Code Online (Sandbox Code Playgroud)
问题:有没有办法指定标签映射进入ggplot?
lazy.labels <- c(
sp ='species',
sep.len='sepal length',
sep.wid='sepal width',
pet.len='petal length',
pet.wid='petal width'
)
Run Code Online (Sandbox Code Playgroud)
并做一些类似的事情
p + labs(lazy.labels)
Run Code Online (Sandbox Code Playgroud)
甚至
p + xlab(lazy.labels[..x..]) + ylab(lazy.labels[..y..])
Run Code Online (Sandbox Code Playgroud)
在哪里..x..,..y..是一些自动ggplot变量保存当前X/Y变量的名称?(然后我可以将这些注释放入一个便利函数中而不必为每个图更改它们)
当我在报告中制作许多图时,这尤其有用.我总是可以ir使用"用户友好"列重命名,但之后我必须做很多事情
ggplot(ir, aes(x=`sepal length`, y=`sepal width`, ...
Run Code Online (Sandbox Code Playgroud)
由于所有的空间,这有点麻烦.
我是R的新手,真的不确定如何在日期框架中过滤数据。
我创建了一个包含两列的数据框,包括每月日期和相应的温度。它的长度为324。
> head(Nino3.4_1974_2000)
Month_common Nino3.4_degree_1974_2000_plain
1 1974-01-15 -1.93025
2 1974-02-15 -1.73535
3 1974-03-15 -1.20040
4 1974-04-15 -1.00390
5 1974-05-15 -0.62550
6 1974-06-15 -0.36915
Run Code Online (Sandbox Code Playgroud)
过滤规则是选择大于或等于0.5度的温度。另外,它必须至少连续5个月。
我已经消除了温度低于0.5度的数据(请参见下文)。
for (i in 1) {
el_nino=Nino3.4_1974_2000[which(Nino3.4_1974_2000$Nino3.4_degree_1974_2000_plain >= 0.5),]
}
> head(el_nino)
Month_common Nino3.4_degree_1974_2000_plain
32 1976-08-15 0.5192000
33 1976-09-15 0.8740000
34 1976-10-15 0.8864501
35 1976-11-15 0.8229501
36 1976-12-15 0.7336500
37 1977-01-15 0.9276500
Run Code Online (Sandbox Code Playgroud)
但是,我仍然需要连续提取5个月。我希望有人能帮助我。
语言是R.
我有一个nxm矩阵,我想将它分成3x3部分并计算每个部分的平均值(或任何函数).(如果有一个剩余的位不是3x3那么就使用剩下的东西).
我确信有一种apply方法可以做到这一点 - 这是我的舌头 - 但我的大脑目前正在让我失望.我想这有点像移动窗口问题,除了我想要非重叠的窗口(所以它更容易).
任何人都可以想到这样做的内置函数吗?还是矢量化的方式?
这是我的循环版本:
winSize <- 3
mat <- matrix(runif(6*11),nrow=6,ncol=11)
nr <- nrow(mat)
nc <- ncol(mat)
outMat <- matrix(NA,nrow=ceiling(nr/winSize),
ncol=ceiling(nc/winSize))
FUN <- mean
for ( i in seq(1,nr,by=winSize) ) {
for ( j in seq(1,nc,by=winSize) ) {
# work out mean in 3x3 window, fancy footwork
# with pmin just to make sure we don't go out of bounds
outMat[ ceiling(i/winSize), ceiling(j/winSize) ] <-
FUN(mat[ pmin(i-1 + 1:winSize,nr), pmin(j-1 + 1:winSize,nc)])
}
} …Run Code Online (Sandbox Code Playgroud) 我想了解我的makefile目标的prequisite如何重新使用%符号,假设目标是X.pdf,与prerequesite是X/X.tex.
详细说来,我目前有一个像这样的makefile:
all: foo.pdf
%.pdf: %.tex
pdflatex $*.tex
Run Code Online (Sandbox Code Playgroud)
我还有一个文件foo.tex,当我键入make它时,它将foo.pdf通过运行pdflatex foo.tex.
现在由于各种原因我无法控制,我的目录结构发生了变化:
my_dir
|- Makefile
|- foo
|- foo.tex
Run Code Online (Sandbox Code Playgroud)
我想修改我的Makefile,这样当它尝试制作时X.pdf,它会查找该文件X/X.tex.
我尝试了以下(我试图把'%/%.tex'告诉它寻找foo/foo.tex):
all: foo.pdf
%.pdf: %/%.tex
pdflatex $*/$*.tex
Run Code Online (Sandbox Code Playgroud)
但是,这会产生:
No rule to make target `foo.pdf', needed by `all'. Stop.
Run Code Online (Sandbox Code Playgroud)
那样做%.pdf: $*/$*.tex.
如果我改变它%/%.tex,foo/%.tex它按预期工作,但我不想硬编码foo在那里,因为在未来我会做all: foo.pdf bar.pdf,它应该寻找foo/foo.tex和bar/bar.tex.
我是相当新的Makefile文件(经验仅限于修改别人的我的需要),也从来没做过更比绝对基础之一,因此,如果任何人都可以给我一个指针,这将有助于(我真的不知道搜索什么词为使Makefile文件中-这看起来前途无量唯一的是%和$*,我不能去上班).
我正在尝试使用JSDoc(3)来记录Javascript文件,如下所示:
/** 1 if gnome-bluetooth is available, 0 otherwise
* @type {boolean}
* @const
*/
const HAVE_BLUETOOTH = @HAVE_BLUETOOTH@;
Run Code Online (Sandbox Code Playgroud)
现在文件(被叫config.js.in)不是自己的有效Javascript; 该文件通过Makefile运行,该文件替换适当的值@HAVE_BLUETOOTH@.
当我尝试在这上面运行JSdoc时,由于文件中的语法错误,它(可以理解)很糟糕.
有没有办法告诉JSDoc忽略此文件中的所有代码,只是考虑到注释?(我可能需要为@name每个doclet 添加标签,以便将文档与代码完全分开;这很好).
就像是:
/** 1 if gnome-bluetooth is available, 0 otherwise
* @name HAVE_BLUETOOTH
* @type {boolean}
* @const
*/
/** @ignore */ // somehow ignore from here onwards
const HAVE_BLUETOOTH = @HAVE_BLUETOOTH@;
/** !@ignore */ // somehow don't ignore from here onwards (although I'd be happy
// to …Run Code Online (Sandbox Code Playgroud) 我想知道是否有R语法从矩阵中提取列并且返回的向量上始终没有name属性(我希望依赖于此行为).
我的问题是以下不一致:
myMatrix[, 1]我会得到第一列myMatrix没有name属性.这就是我要的.myMatrix[, 1],我会得到第一列,myMatrix 但它有第一个colname作为它的名字.我希望能够做到myMatrix[, 1]并且始终如一地得到没有名字的东西.
举例说明:
# make a matrix with more than one row,
x <- matrix(1:2, nrow=2)
colnames(x) <- 'foo'
# foo
# [1,] 1
# [2,] 2
# extract first column. Note no 'foo' name is attached.
x[, 1]
# [1] 1 2
# now suppose x has just one row (and is a matrix) …Run Code Online (Sandbox Code Playgroud) 我正在尝试在R中进行一些模糊匹配,其中我有多个数据字段要匹配.
例如:
try_to_match <- c('seoul korea', 'bisbane', 'korea', 'australia brisbane')
locations <- data.frame(name=c('seoul', 'brisbane'),
country=c('south korea', 'australia'))
Run Code Online (Sandbox Code Playgroud)
我想将用户输入的位置try_to_match与locations数据帧匹配.
现在,关于SO上的R模糊匹配存在类似的问题,并且大多数都是覆盖agrep.但是,当有多个要匹配的单词时,我找不到任何覆盖模糊匹配的内容.
例如,如果我只是对抗locations$name,我会得到"bisbane"到"brisbane"的匹配,正如我所期待的那样.此外,我对其中的国家/地区的各种搜索没有匹配,因为其中locations$name没有国家/地区.
sapply(try_to_match, agrep, locations$name, value=T)
# $`seoul korea`
# character(0)
# $bisbane
# [1] "brisbane"
# $korea
# character(0)
# $`australia brisbane`
# character(0)
Run Code Online (Sandbox Code Playgroud)
所以,我想我也应该与国家相匹配:
sapply(try_to_match, agrep, paste(locations$name, locations$country), value=T)
# $`seoul korea`
# character(0)
# $bisbane
# [1] "brisbane australia"
# $korea
# [1] "seoul south korea"
# $`australia brisbane` …Run Code Online (Sandbox Code Playgroud) 我有一个字符串:
string <- "I do not like green eggs and ham!"
Run Code Online (Sandbox Code Playgroud)
和一个图案
pattern <- "(egs|ham)"
Run Code Online (Sandbox Code Playgroud)
我想知道有多少次pattern匹配string与模糊匹配(AGREP) 。
gregexpr将为正常匹配执行此操作 - 我只想知道garegexprR 中是否有相应的方法或一种模拟它的方法,而不会影响性能。
(aregexec只会返回第一个匹配项的索引,“eggs”,并跳过“ham”)。
TL; DR:我怎么能传递fun.aggregate到dcast.data.table时调用dcast.data.table的函数(给我传内完成fun.aggregate)?
我有一张这样的表:
library(data.table)
t <- data.table(id=rep(1:2, c(3,4)), k=c(rep(letters[1:3], 2), 'c'), v=1:7)
t
id k v
1: 1 a 1
2: 1 b 2
3: 1 c 3
4: 2 a 4
5: 2 b 5
6: 2 c 6
7: 2 c 7 # note the duplicate (2, c)
Run Code Online (Sandbox Code Playgroud)
我重塑为长格式,保留最后一次出现的重复项
dcast.data.table(t, id ~ k, value.var='v', fun.aggregate=last) # last is in data.table
id a b c
1: 1 1 2 3
2: …Run Code Online (Sandbox Code Playgroud) 在rmarkdown中,您可以创建一个_output.yaml仅具有输出格式的通用选项的文件(参考)
html_document:
toc: true
pandoc_args: ['--base-header-level', 2]
pdf_document:
toc: true
pandoc_args: ['--base-header-level', 2]
Run Code Online (Sandbox Code Playgroud)
我的问题是,是否可以在_output.yaml中共享非输出选项?更接近标准rmarkdown中用作frontmatter的格式.
例如:
bibliography: ../library.bib
author: me
output:
html_document:
toc: true
pandoc_args: ['--base-header-level', 2]
pdf_document:
toc: true
pandoc_args: ['--base-header-level', 2]
Run Code Online (Sandbox Code Playgroud)
这样我render在该目录中的任何文件都会自动获得作者'我',指定的参考书目路径和输出选项?(目前如果我这样做,它会抱怨一个eval电话,基本上是试图评估我的第一个选项的价值."object '..' not found").
避免在单个文件中复制某些非输出格式特定的选项(例如作者,参考书目)会很方便.
我知道我可以通过(ab)使用来解决这个问题pandoc_args,因为它是特定于格式的,在放入时会起作用_output.yaml.例如,["--bibliography", "./library.bib", "--variable", "author:me"]在这种情况下会起作用,但可能并非在所有情况下都适用.
如果我可以将多个文档串在一起以连接成单个文档,这也可以解决; 这是做什么的pandoc.例如pandoc config.yaml chapter1.md chapter2.md有效,但render(c('config.yaml', 'chapter1.md', 'chapter2.md'))没有.
旁白:有没有办法为所有输出格式设置默认选项(例如,我想toc: true默认使用所有输出格式)而不必复制它?
r ×8
agrep ×1
bash ×1
data.table ×1
dataframe ×1
filter ×1
fuzzy-search ×1
ggplot2 ×1
jsdoc ×1
makefile ×1
matrix ×1
r-markdown ×1
subset ×1
unix ×1
vector ×1