title: Swapping specified rows in a dataframe
body: <p>I am doing a homework problem where we create a dataframe with 4 columns and 10 rows and then create a function to deal with the dataframe. </p>
  
  <p>The problem states: 
  Write a single function in R called “transform” that does the following:
  Accepts four arguments (without default values) in the following order:
  df: a dataframe.
  a: an integer indicating a chosen row number.
  b: an integer indicating a second/different chosen row number.
  varName: The name of any variable in df.</p>
  
  <p>The function should:
  Ensure that a and b are valid row numbers.
  If they are valid the function should swop the value in the chosen variable at row number 'a' with row number 'b'.</p>
  
  <p>The code needs to be a single function. </p>
  
  <p>The code should work for a dataframe with different column names as well. </p>
  
  <pre><code>transform &lt;- function(df, a, b, varName){
  
    if(a &gt; nrow(df) | b &gt; nrow(df)){
      print('Invalid row numbers specified')
    }
  
    if(a &lt; 1 | b &lt; 1){
      print('Invalid row numbers specified')
    }
  
    v &lt;- varName
    c &lt;- which(colnames(df) == v)
  
    if(c == 1){
      x &lt;- df[a, 1]
      y &lt;- df[b, 1]
      df[a, 1] &lt;- y
      df[b, 1] &lt;- x
    }
  
    if(c == 2){
      x &lt;- df[a, 2]
      y &lt;- df[b, 2]
      df[a, 2] &lt;- y
      df[b, 2] &lt;- x
    }
  
    if(c == 3){
      x &lt;- df[a, 3]
      y &lt;- df[b, 3]
      df[a, 3] &lt;- y
      df[b, 3] &lt;- x
    }
  
    if(c == 4){
      x &lt;- df[a, 4]
      y &lt;- df[b, 4]
      df[a, 4] &lt;- y
      df[b, 4] &lt;- x
    }
  }
  </code></pre>
  
  <p>Expected results:</p>
  
  <pre><code>Num &lt;- c(1:10)
  Age &lt;- c(14,12,15,10,23,21,41,56,78,12)
  Sex &lt;- c('F','M','M','F','M','F','M','M','F','M')
  Group &lt;- letters[1:10]
  datfr &lt;- data.frame(Num, Age, Sex, Group)
  
  datfr &lt;- transform(datfr,1,3,"Group")
  datfr &lt;- transform(datfr,7,2,"Group")
  datfr &lt;- transform(datfr,5,10,"Group")
  datfr &lt;- transform(datfr,5,11,"Group")
  [1] "ERROR: Invalid row numbers specified"
  datfr
      Num      Age     Sex        Group
  1    1       14       F           c
  2    2       12       M           g
  3    3       15       M           a
  4    4       10       F           d
  5    5       23       M           j
  6    6       21       F           f
  7    7       41       M           b
  8    8       56       M           h
  9    9       78       F           i
  10   10      12       M           e
  </code></pre>
  
  <p>Actual results:</p>
  
  <pre><code>Num &lt;- c(1:10)
  Age &lt;- c(14,12,15,10,23,21,41,56,78,12)
  Sex &lt;- c('F','M','M','F','M','F','M','M','F','M')
  Group &lt;- letters[1:10]
  datfr &lt;- data.frame(Num, Age, Sex, Group)
  
  datfr &lt;- transform(datfr, 1, 3, "Group")
  datfr &lt;- transform(datfr, 7, 2, "Group")
  Error in if (a &gt; nrow(df) | b &gt; nrow(df)) { : argument is of length zero
  datfr &lt;- transform(datfr, 5, 10, "Group")
  Error in if (a &gt; nrow(df) | b &gt; nrow(df)) { : argument is of length zero
  datfr &lt;- transform(datfr, 5, 11, "Group")
  Error in if (a &gt; nrow(df) | b &gt; nrow(df)) { : argument is of length zero
  datfr
  [1] a
  Levels: a b c d e f g h i j
  </code></pre>