让我们假设我是一个有着坏习惯的懒惰程序员(他也碰巧不知道plyr/dplyr),我喜欢做这样的操作:
`[<-`((z<-reshape2::melt(iris)), z[,"value"]>5, 3, 100)
Run Code Online (Sandbox Code Playgroud)
熔化iris,然后将值100分配给value大于5的行,然后返回所有行,而不仅仅是选定的行.该功能在页面上描述?"["
相同的代码replace()(几乎相同)
z[,"value"] <- replace(i <- ((z <- reshape2::melt(iris))[,"value"]), i > 5, 100)
Run Code Online (Sandbox Code Playgroud)
1)但问题是:有没有办法[<-使用标准括号表示法调用函数iris[<-, blah, blah, blah, ?]?
编辑2016年7月:所以这个问题的目的不是复制操作.数据并不重要,例子无关紧要,重塑数据的复杂方式无关紧要.
这个问题在参考时是对代码 - 高尔夫挑战的观察.
提交的R解决方案是一个有效的解决方案,但是我们中的一些人(可能只是我)似乎对为什么需要进行初始X=m重新分配感到茫然.
代码由@Giuseppe高调一点,所以我会给读者写一些评论.
function(m){
X=m
# Re-assign input m as X
while(any(X-(X=X%*%m))) 0
# Instead of doing the meat of the calculation in the code block after `while`
# OP exploited its infinite looping properties to perform the
# calculations within the condition check.
# `-` here is an abuse of inequality check and relies on `any` to coerce
# the numeric to logical. See `as.logical(.Machine$double.xmin)`
# The code basically multiplies the matrix …Run Code Online (Sandbox Code Playgroud) floating-point r matrix-multiplication stochastic-process stochastic
看这里的情节:
(从这里)
如何使用ggplot2重现条形图的上部和下部?
例如,我可以生产上部
ggplot(data.frame(x=rnorm(1000, 5)), aes(x=x)) + geom_bar() + scale_y_reverse()
Run Code Online (Sandbox Code Playgroud)
但是现在,如果我添加任何其他geom_,例如另一个geom_bar(),y的比例被反转.是否可以scale_y_reverse()仅应用于特定的geom_?
我如何编写一个正则表达式来抓取位于任何后续字符之外的大写字母直到空格?
输入:
cake pietypeAPPLE CRUMBLE tart toastTexas price
例如,我想抓住"APPLE",尽管它没有前面的空格.我想要"CRUMBLE".我也想要"德克萨斯",即使它的所有组件都不是大写的.
我将用于gsub(pattern, replacement = "", x = string)获得以下输出
输出:
cake pietype tart toast price
谢谢!
美好的一天,我有一个来自txt文件的数据集
> MyData
Xdat Ydat
1 1 12
2 2 23
3 3 34
4 4 45
5 5 56
6 6 67
7 7 78
Run Code Online (Sandbox Code Playgroud)
我需要使用此set来提取与第二列(Ydat)大于40的情况相对应的行.导致MyData2
Xdat Ydat
4 4 45
5 5 56
6 6 67
7 7 78
Run Code Online (Sandbox Code Playgroud)