0

I have a data frame:

df <- data.frame( a = 1:5, b = 1:5, c = 1:5, d = as.factor(1:5))

I want to write a function that takes as its argument one of the columns a,b or c, and one of the factors of column d, and returns only the values of column a, b, or c, that have said factor value for column d.

I tried the following code:

fun1 <- function(x,y) {
      u <- x[data$d == "y"]
       return(u)
   }

and I keep getting back numeric(0) as the output of the function. When I try similar code outside of the function() environment, it appears to work fine. Any help would be appreciated.

Eric Fail
  • 8,191
  • 8
  • 72
  • 128
  • note that `df` is an existing command (the F distribution), so you should probably use another variable name. – dfrib Jan 21 '16 at 22:03
  • df was just for the example, it's called something differently in my real code. – user154334 Jan 21 '16 at 22:04
  • Can you show your intended result for the example data? – Thomas Jan 21 '16 at 22:05
  • Should you have a comma inside the dataframe brackets in your function? i.e. `u <- x[data$d == "y", ]` Also, what is `data` in your function? – oshun Jan 21 '16 at 22:09
  • For my intended result, I just want a list of the variables of the column you designate that also have the factor you designate for column d. When I add a comma it says "incorrect number of dimensions" – user154334 Jan 21 '16 at 22:12
  • You need to post the actual _code_ you intend to use. At the moment it's unclear how your column specification was intended to be chosen. – IRTFM Jan 21 '16 at 23:45

1 Answers1

1

Probably a duplicate but I don't know how I would find it in the haystack of items with tags: data.frame, indexing, columns, values. Best practice is to pass the "data" as well as the search terms. (Calling the object df1 rather than df.)

 fun1 <- function(dfrm, col,val) {
       u <- dfrm[dfrm$d == val , col]
        return(u)
    }
fun1(df1, 'b', 3)
#[1] 3
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks for the reply. When I try this with my actual data, I get Error in `[.data.frame`(dfrm, dfrm$d == val, col) : object 'factornamehere' not found – user154334 Jan 21 '16 at 22:49
  • @42- It sure is a dupe. I only know this because I've answered it a bunch. Here is the 42- way http://stackoverflow.com/questions/34935313/how-to-create-a-for-loop-in-r-for-this-peculiar-calculation. Here is a more advanced approach using non-standard evaluation http://stackoverflow.com/questions/19133980/passing-a-variable-name-to-a-function-in-r/34079727#34079727 – Jacob H Jan 21 '16 at 22:55
  • We can only know if the second one is a dupe if the OP actually posts the calling method he had in mind ... rather than posting reports of errors without the supporting code. – IRTFM Jan 21 '16 at 22:59
  • @42- good point on the second. Here is a better example using non-standard evaluation http://stackoverflow.com/questions/34076956/subset-dataframe-based-on-column-value-using-a-function-in-r/34077429#34077429 – Jacob H Jan 21 '16 at 23:19