-2

All other questions and answers I have seen on this have not been useful.

I have imported data from an Excel sheet into R and I want to remove all of the non-numeric data from a particular column so I can perform calculations. Ideally I would like to be able to create a function that does this.

Assuming the data frame is called price and the column I want is Q1: I have found answers to this question where I could do this by using

  • convert <- as.numeric(as.character(price$Q1)
  • ncolumn <-concert[!is.na(convert)]

However, when I try and create the function I want to have the inputs to be both the name of the data frame as well as the name of the column. I have tried using price[2] instead of price$Q1 in that first line I showed but it doesn't seem to work. I also tried extracting the name of the column and the name of the data frame and using the $ notation instead of [ ] and it still doesn't work.

D Gordon
  • 47
  • 4
  • 3
    Welcome to SO and thank you for posting. Here are a couple of tips for improving your question from the [Help Conter](http://stackoverflow.com/help/how-to-ask). You would help people to answer your question if you describe why the other post didn't work and link back to them. Also, including a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would help people to help you. – Richard Erickson May 19 '15 at 23:24
  • 1
    To reiterate the last point: "Assuming" X, Y, Z about your data does not go very far towards explaining the problem you are facing; while a small example probably would. – Frank May 19 '15 at 23:28
  • 1
    Post the output of `dput(head(price) )` by editing your question (NOT in a comment). – IRTFM May 20 '15 at 00:58

1 Answers1

0

Guessing from the question, would the below be what you are trying to do?

#function with inputs of both name of a data frame and name of a column

NumConv <- function(data, column){  
convert<-as.numeric(as.character(data[, column]))  
convert[!is.na(convert)]  
}

#executing the function with your assumed example

NumConv(price, "Q1")
Eddy
  • 46
  • 2