R is a programming language, you can make your own function or package. Furthermore, if you are a advanced R programmer, you can contribute R package list by submitting and registering your packages to CRAN, then everybody will use your package.
Today I will make a very simple function.
As you are aware, function is a basic unit of R command. you can select and download any packages from CRAN site and then you can use appropriate function for your own purpose.
Let's make a simple scenario.
I have a data and I would like to add all each number for specific times
(1) My data : 1,2,3,4,5,6,7,8,9,10
(2) Expected result : (present result by specific times)
3 4 5 6 7 8 9 10 11 12 (Add 2 times)
.
.
.
16 17 18 19 20 21 22 23 24 25 (Add 15 times)
.
.
Let's begin to make your own function,Making a new function is very simple
Like other assign operations, new function code should be assigned new function name.
and function logic is
* As you can see, I use for statement, I will explain "control statement" next post.
[R code]
Addyourdata <- function (x, N) {
for (j in 1:N) {
j=j+1
x=x+1
}
return(x)
}
Now I have my own function.
x is a input variable for your data
N is also another input variable for number of times you want to
Just writing this function name displays function logic again.
It's time to verify our function works properly.
> Addyourdata(basicdata,2)
[1] 3 4 5 6 7 8 9 10 11 12
> Addyourdata(basicdata,10)
[1] 11 12 13 14 15 16 17 18 19 20
> Addyourdata(basicdata,15)
[1] 16 17 18 19 20 21 22 23 24 25
>
> Addyourdata(basicdata,100)
[1] 101 102 103 104 105 106 107 108 109 110
> Addyourdata(basicdata,1880)
[1] 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
>
It works ,great!
댓글 없음:
댓글 쓰기