显然,我没有得到grep在R中的工作方式.如果我在OS X终端上使用grep,我可以使用参数-o,这使得grep只返回匹配的部分.在R中,我找不到如何做相应的事情.阅读手册我认为值是正确的方法,这更好,因为它返回字符而不是索引,但仍返回整个字符串.
# some string fasdjlk465öfsdj123
# R
test <- fasdjlk465öfsdj123
grep("[0-9]",test,value=TRUE) # returns "fasdjlk465öfsdj123"
# shell
grep -o '[0-9]' fasdjlk465öfsdj123
# returns 4 6 5 1 2 3
Run Code Online (Sandbox Code Playgroud)
R中缺少的参数是什么?
编辑:Joris Meys的建议非常接近我想要做的事情.我得到一个矢量作为readLines的结果.我想检查向量的每个元素是否有数字并返回这些数字.我真的很惊讶没有标准的解决方案.我想过使用一些可以在字符串上运行的regexp函数并像grep -o一样返回匹配,然后在该向量上使用lapply.grep.custom最接近 - 我会尽力为我工作.
我有一组R对象,c1,c2,c3,...,c10每个对象都有相同的长度,并喜欢从这些对象形成一个数据帧.我可以做到df<-data.frame(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10).有没有更有效的方法来做到这一点,也许使用paste?
谢谢你的帮助.
我是R.的新手.现在我的功能如下:
funItemAverRating = function()
{
itemRatingNum = array(0, itemNum);
print("begin");
apply(input, 1, function(x)
{
itemId = x[2]+1;
itemAverRating[itemId] <<- itemAverRating[itemId] + x[3];
itemRatingNum[itemId] <<- itemRatingNum[itemId] + 1;
}
);
}
Run Code Online (Sandbox Code Playgroud)
在这个函数中输入是一个n*3数据框,n是~6*(10e+7),itemRatingNum是一个大小的向量~3*(10e+5).
我的问题是为什么apply功能这么慢(完成需要将近一个小时)?此外,随着函数的运行,它会使用越来越多的内存.但正如您所看到的,变量都是在apply函数外部定义的.有谁能够帮我?
程
当我使用时,这个功能如何适用于所有文本框
$('#date, #date2').blur(function () {
但不是我用的时候
$('#date').blur(function () { ?
MVC查看器:
@Html.TextBoxFor(x => x.ArrivalDateStart, new { id = "date" })
@Html.TextBoxFor(x => x.ArrivalDateEnd, new { id = "date" })
@Html.TextBoxFor(x => x.OtherDateEnd, new { id = "date" })
@Html.TextBoxFor(x => x.OtherDateEnd, new { id = "date" })
Run Code Online (Sandbox Code Playgroud)
使用Javascript:
$(function () {
$('#date, #date2').blur(function () {
var date = $(this).val();
var result = "";
if (date.length === 8) {
result = result += date.substring(0, 4) + "-" + date.substring(4, 6) + …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个创建索引的函数(从100开始),然后根据投资结果调整此索引.因此,简而言之,如果第一笔投资的利润为5%,那么指数将为105,如果第二个结果是-7%,则指数为97.65.在这个问题中,当我使用"索引"这个词时,我并不是指包的index功能zoo.
除了创建这个索引之外,我的目标还是创建一个可以应用于我的完整数据集的各种子集的函数(即使用sapply它和它的朋友).
这是我到目前为止的功能(此问题末尾的数据):
CalculateIndex <- function(x){
totalAccount <- accountValueStart
if(x$TradeResult.Currency == head(x$TradeResult.Currency., n = 1)){
indexedValues <- 100 + ( 100 *((((x$Size.Units. * x$EntryPrice) / totalAccount) * x$TradeResult.Percent.) / 100))
# Update the accountvalue
totalAccount <- totalAccount + x$TradeResult.Currency.
}
else{ # the value is not the first
indexedValues <- c(indexedValues,
indexedValues[-1] + (indexedValues[-1] *(((x$Size.Units. * x$EntryPrice) / totalAccount) * x$TradeResult.Percent.) / 100)
)
# Update the accountvalue
totalAccount <- …Run Code Online (Sandbox Code Playgroud)