在R中,计算序列中相同元素运行的最有效/最简单的方法是什么?
例如,如何计算非负整数序列中连续零的数量:
x <- c(1,0,0,0,1,0,0,0,0,0,2,0,0) # should give 3,5,2
Run Code Online (Sandbox Code Playgroud) 这就是我想要$\mathcal {l} =\mathcal {L} $来生成两个字母.我知道\ mathcal只对大写字母起作用(可预测).谢谢.
只是为了帮助那些刚刚自愿删除问题的人,按照他试过的代码请求和其他评论.我们假设他们尝试过这样的事情:
str <- "How do I best try and try and try and find a way to to improve this code?"
d <- unlist(strsplit(str, split=" "))
paste(d[-which(duplicated(d))], collapse = ' ')
Run Code Online (Sandbox Code Playgroud)
并希望学习更好的方法.那么从字符串中删除重复单词的最佳方法是什么?
给定数据
s<-c(1,0,0,0,1,0,0,0,0,0,1,1,1,0,0)
Run Code Online (Sandbox Code Playgroud)
我可以用table或ftable计算1和0
ftable(s,row.vars =1:1)
Run Code Online (Sandbox Code Playgroud)
并且总共有11s,01s,10s,00s发生在s中
table(s[-length(s)],s[-1]).
Run Code Online (Sandbox Code Playgroud)
什么是聪明的方法来计算111s,011s,...,100s,000s的出现次数?理想情况下,我想要一个x表的计数表
0 1
11 x x
01 x x
10 x x
00 x x
Run Code Online (Sandbox Code Playgroud)
是否有一般方法计算长度为k = 1,2,3,4,......的所有可能子序列的总出现次数?
是否有更"有思想"的方式来有效地进行二分法?谢谢.
y<-c(0,3,2,1,0,0,2,5,0,1,0,0);b<-vector()
for (k in 1:length(y)) {
if (y[k] == 0) b[k] = 0
else
b[k] = 1
}
y;b
Run Code Online (Sandbox Code Playgroud) data <-c(88, 84, 85, 85, 84, 85, 83, 85, 88, 89, 91, 99, 104, 112, 126, 138, 146,151, 150, 148, 147, 149, 143, 132, 131, 139, 147, 150, 148, 145, 140, 134, 131, 131, 129, 126, 126, 132, 137, 140, 142, 150, 159, 167, 170, 171, 172, 172, 174, 175, 172, 172, 174, 174, 169, 165, 156, 142, 131, 121, 112, 104, 102, 99, 99, 95, 88, 84, 84, 87, 89, 88, 85, 86, 89, 91, 91, 94, 101, 110, …Run Code Online (Sandbox Code Playgroud) 报告二进制序列时,空间是多余的.这段代码
x <- '1 0 0 0 0 0 1 1 0 1 0 1 1 0 '
y<-gsub(' +', '', x)
Run Code Online (Sandbox Code Playgroud)
完成这项工作,以便我可以从R复制和粘贴.如何对其他格式的0-1序列(和其他一位数据)执行相同的操作,例如,
x <- c(1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0)
Run Code Online (Sandbox Code Playgroud)
要么
toString(x)
Run Code Online (Sandbox Code Playgroud)
或者其他什么(为了学习各种选择)?谢谢.
我正在jekyll内学习流动性,并且很难按月获取职位数量。似乎很容易计算每个标签或类别的帖子数(因为有变量site.tags和site.categories),而我没有遇到任何问题。这是我的实时示例,可以在github上获得用于统计每个标签/类别的帖子的源代码。为了按月计数帖子,我尝试使用类似
{% capture counter %}{{ counter | plus:1 }} {% endcapture %} {% endif %}
Run Code Online (Sandbox Code Playgroud)
但是它的各种用法并没有给我预期的效果,我现在怀疑有更好的方法。问题是我怎么可能修改下面的代码,以便显示月份(给定年份)而不是每个类别的帖子数?
{% capture site_cats %}{% for cat in site.categories %}{{ cat | first }}
{%unless forloop.last %},{% endunless %}{% endfor %}{% endcapture %}
{% assign sortedcats = site_cats | split:',' | sort %}
{% for category in sortedcats %}
{{category }}{{site.categories[category] | size }}
<ul>
{% for post in site.categories[category] %}
{% if post.url %}
<li><a href="{{ post.url }}">{{ post.title }}</a> …Run Code Online (Sandbox Code Playgroud) R问题:寻找最快的方法来数值地解决一堆已知具有真实系数和三个真实根的任意立方体.据报道,R中的多根函数使用Jenkins-Traub的算法419用于复数多项式,但对于实数多项式,作者参考了他们早期的工作.对于真实的多项式,或者更普遍的真实多项式,有哪些更快的选项?
将非递减seq的出现次数转换为0-1 seq有哪些更好的选择?谢谢.
d<-c(3,5,9,12,15);
c(rep(0,d[1]-1),1,unlist(rbind(mapply(rep,0,diff(d)-1),1)))
Run Code Online (Sandbox Code Playgroud) r ×9
count ×3
sequence ×2
binary ×1
duplicates ×1
fonts ×1
forecasting ×1
jekyll ×1
latex ×1
liquid ×1
performance ×1
random ×1
spaces ×1