It seems like one of the primary things I get stuck on when R programming is passing through variable names. I come from a Stata background, where we can easily call globals with "$" in any code or function. However, that doesn't seem to work in R. It seems like sometimes I have to use some special package or use something like df[[x]] or something like that. Instead of doing all of this ad-hoc, I was wondering if someone can walk me through the R architecture so I understand how to address this problem every time I run into it.
As a simple example, I am currently working on a code that stores a row count:
rowcount <- function(x){
all_n <- length(which(!is.na(df$x) & df$model=="Honda"))
print(all_n)
}
The function simply stores the count of rows when x is not missing and make is "Honda". I want to be able to pass the variable name into the function, then have it return this count. For instance, for variable gender, I want to be able to write rowcount(gender)', and for gender to be passed into the function as
df$gender'. However, this doesn't happen.
Can someone explain how to fix this code, and in the process, how I can generally fix these types of problems? I know there may be more elegant ways to achieve my goal, but my intention is both to (1) get a code that fulfills a specific goal for my project, and (2) more generally understand how R treats variable names as arguments in functions.
Thanks