0

I have a piece of code that works fine outside the function but not inside the function(R programming language).

I am reading an Excel file with:

myFile = read.csv2("myFile.csv", na.strings = c("-", "k.A")) 

There are a number of columns in this file like gender(Geschlecht), grades, semesters etc...

I then run the following code which gives me the bar plot for the gender column.

barplot(prop.table(table(myFile$Geschlecht)))

I want to make a function that I can use to print a barplot for any column, as long as I know the name of the column heading.

This is the function I made:

mybarplot = function(dataCat, Title){
  barplot(prop.table(table(myFile$dataCat)))
}

I then call the function like this:

mybarplot(Geschlecht,"Geschlecht")

This gives me the following error and warnings:

*Error in plot.window(xlim, ylim, log = log, ...) : 
  need finite 'xlim' values
In addition: Warning messages:
1: In min(w.l) : no non-missing arguments to min; returning Inf
2: In max(w.r) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) :
 Show Traceback

 Rerun with Debug
 Error in plot.window(xlim, ylim, log = log, ...) : 
  need finite 'xlim' values* 

As far as I can see, this has something to do with the NA values in my file? These values are specified by k.A or - in my Excel file(German language in case you are wondering).

I would appreciate if someone can point me in the right direction so I can take care of this as i'm stuck on this since day before yesterday.

mukund
  • 553
  • 3
  • 15
detraveller
  • 285
  • 3
  • 17
  • 1
    Unfortunately, R wont accept variable names for subsetting when using `$` syntax. See https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-vector-of-column-names – anotherfred Dec 24 '18 at 17:03
  • 2
    Change `myFile$dataCat` to `myFile[[dataCat]]` – anotherfred Dec 24 '18 at 17:07
  • @anotherfred it says object 'Geschlecht' not found. And I am not exactly sure what your first comment means but im trying to find out more about it. – detraveller Dec 24 '18 at 17:15
  • When you call the function, Geshlect needs to be in quotes. – anotherfred Dec 24 '18 at 17:18
  • thanks. It works now. I'll now try to learn the difference between the $ operator and the [] thing using the link you provided. – detraveller Dec 24 '18 at 17:24

1 Answers1

1

To call columns using dollar sign '$' is sometimes being picky. Please try to select column by data[[column]] (like @anotherfred said) or function subset subset(myFile, select=Geschlecht) and don't forget to convert variables format into numeric as.numeric() if the column you attempt to apply has digits.