如何结合量身定制的功能?

时间:2016-01-11 10:33:39

标签: r

我有以下数据(数据通过reshape2融化)。数据看起来像这样。数据名称为k。

@echo %1

SET ProjectPath=%1
SET VsTest=vstest.console.exe %ProjectPath% /Logger:trx


SET TestWindowPath="C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow"

SET TestResults=C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\TestResults\

cd /d %TestWindowPath%

%VsTest%

cd /d %TestResults%

for /f "delims=" %%x in ('dir /od /a-d /b *.*') do set RECENT=%%x
echo %RECENT%

SET trx=trx2html.exe "%RECENT%"

%trx%

我写了这段代码来从值中提取第一个数字和最后一个数字:

 variable       value
 Revenue        23.34
 Revenue        34.44
 Revenue         13

也许我需要尝试这种方法。确保要求所有库。

require(plyr)
require(stringr)
k <- ddply(k, .(variable), transform, 
           first.digit = str_extract(value, "[123456789]"),
           last.digit =   str_extract(value, "[[:digit:]]$"))  

应用代码后,数据如下所示:

k_function <- function(data){
require(plyr)
require(stringr)
ddply(data, .(variable), transform, 
      first.digit = str_extract(value, "[123456789]"),
      last.digit =   str_extract(value, "[[:digit:]]$")) -> k_data
return(k_data)
}

如何将整个程序整合到量身定制的功能中。

1 个答案:

答案 0 :(得分:2)

这将有效(您可以阅读如何编写函数,例如:http://www.r-bloggers.com/how-to-write-and-debug-an-r-function/):

my_function <- function(data){

    ddply(data, .(variable), transform, 
          first.digit = str_extract(value, "[123456789]"),
          last.digit =   str_extract(value, "[[:digit:]]$")) -> new_data
    return(new_data)
}

my_function(k)