-3

I have a csv file which contains two columns: A1 and A2. Supposing I have the following function:

WKH <- function(A, B){
                       S <- ((B-A)-(B+A)/2
                       return(S)
                       }

So how can I loop through the columns A and B so that the corresponding WKH can be estimated for all values under the two columns.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
user3770793
  • 1
  • 1
  • 2
  • Isn't this function equivalent to setting `S` to be equal to `-A`? – josliber Jun 22 '15 at 15:40
  • No the idea is to apply the figures in two different columns to estimate WKH and I want to find out how to do something like that – user3770793 Jun 22 '15 at 15:53
  • 1
    This is not clear to me -- please update your question with a reproducible example that include a sample dataset and the expected output. You can read more about creating reproducible examples [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – josliber Jun 22 '15 at 15:54

1 Answers1

0

I'm a bit embarrassed to post this as an answer, it would be better if you had provided some sample data and some actual code, see @jsilber's comment. But I guess that is what you want:

# create a sample dataframe as it might look like after read.csv()
a <- data.frame( A1 = seq( 10, 19 ), A2 = seq( 20, 19 ) )

# perform the operation that you describe, get a vector as result
WKH <- with( a, ( A2 - A1 ) - ( A2 + A1 ) / 2 )

# no loop necessary, you have all you need at hand:
WKH
[1]  -5  -7  -8 -10 -11 -13 -14 -16 -17 -19
vaettchen
  • 7,299
  • 22
  • 41