[R] Creating Functions in R

David L Carlson dcarlson at tamu.edu
Thu Jul 24 16:17:09 CEST 2014

Before you start writing functions, you should learn the basics of R by reading "An Introduction to R" (http://cran.r-project.org/doc/manuals/r-release/R-intro.pdf). Pages 7 and 8 cover what you are asking. There is no need for a for() loop at all and your function is simply overwriting the value of "deviation" so you only get the last value recycled (page 20, "The recycling rule").

David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Pavneet Arora
Sent: Thursday, July 24, 2014 8:36 AM
To: r-help at r-project.org
Subject: [R] Creating Functions in R

Hello Guys
I am new at writing Functions in R, and as a result am struggling with it. 
I am trying to use Google & other resources, but it's hard to find 
solutions when you don't know what to look for. 

I have the following small dataset
> dput(sub)
structure(list(week = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 
29, 30), value = c(9.45, 7.99, 9.29, 11.66, 12.16, 10.18, 8.04, 
11.46, 9.2, 10.34, 9.03, 11.47, 10.51, 9.4, 10.08, 9.37, 10.62, 
10.31, 10, 13, 10.9, 9.33, 12.29, 11.5, 10.6, 11.08, 10.38, 11.62, 
11.31, 10.52)), .Names = c("week", "value"), row.names = c(NA, 
-30L), class = "data.frame")

I want to take each of the value and subtract from a target {in this case 
its 10}. This is what I have written in my function so far:
vmask <- function(data,target){
for(k in 1:length(data))
deviation <- data[k]- target
dev <- return(data.frame(cbind(data,deviation)))

But when I run this I get the results as expected. But I expected the new 
coloumn to be called "deviation", whereas R just calls in "value.1". How 
can I fix this?
Also I was hoping to see this new dataset with columns "week", "value", 
and now "deviation" when I use "View(dev) - but it comes up with error 
'dev not found'. How can i fix this? Also is there anyway instead of me 
making a new dataset called "dev" with the 3 columns, I can just re-use my 
original dataset "sub" and give me all the 3 new columns?

The next step I want to do is to perform a cumulative sum. So looking at 
the results, I want a new coloumn in existing dataset (or new dataset), 
which will now have 4 columns. The 4th column I want to be called "CuSum". 
So the first row of Cusum will be "-0.55", the second = "-0.55+(-2.01)" 
which will give me "-2.56" and so on forth.

How can I do this in R using a function? Please help

MORE TH>N is a trading style of Royal & Sun Alliance Insurance plc (No. 93792). Registered in England and Wales at St. Mark’s Court, Chart Way, Horsham, West Sussex, RH12 1XL. 

Authorised by the Prudential Regulation Authority and regulated by the Financial Conduct Authority and the Prudential Regulation Authority.

	[[alternative HTML version deleted]]

More information about the R-help mailing list