0

I am trying to write this into a function:

dataFrAme[dataFrAme$x == "B" & dataFrAme$`Solvabiliteit: Hoog - Laag` == "Hoog",]

In my function below, I try to select rows based on 'id_type' and 'kern getal' type from a data frame (datframe). To do this I use a fixed column for id_type -> x in this case, and an input varable for the column that contains 'kern_getal_type'.

df_slct <- function(datframe,id_type,kern_getal_type){
  datframe[(datframe$x == id_type) && (datframe$kern_getal_type == "Hoog"),]
}

This input of function is as follows:

df_selectie_risico(dataFrAme,"B",`Solvabiliteit: Hoog - Laag`)

It seems like the second argument in the fuction does not work: Warning message: Unknown or uninitialised column: kern_getal_type.

Does anyone know how to I can use the second column in subsetting the rows?

nemja
  • 459
  • 4
  • 19
  • 1
    Your function is using `&&`, it should be using `&` instead. See https://stackoverflow.com/q/6558921/3358272 – r2evans Jun 01 '23 at 12:07
  • Also, in the function, `kern_getal_type` is a variable to a string, you likely want `datframe[[kern_getal_type]] == "Hoog"` instead. See https://stackoverflow.com/q/18222286/3358272 – r2evans Jun 01 '23 at 12:08
  • Thanks for yor answer! Howerever, the && or & makes no difference. How do I incorporate the [[kern_getal_type]] in my code? Like this it does not go: df_selectie_risico <- function(datframe,id_type,kern_getal_type){ datframe[(datframe$x == id_type) & (datframe$[[kern_getal_type]] == "Hoog"),] } – nemja Jun 01 '23 at 12:11
  • `the && or & makes no difference` ... if you have one row or all values are the same, then it will make no difference. Otherwise they are very different (prove to me that they are identical). How to use it? I showed you, change your `datframe$kern_getal_type == "Hoog"` to `datframe[[kern_getal_type]] == "Hoog"`, is that not clear? Is it not working? – r2evans Jun 01 '23 at 12:20
  • I get the following error: object 'Solvabiliteit: Risico Hoog - Laag' not found – nemja Jun 01 '23 at 12:26
  • I do not understand what you're hoping to do. I suggest you make this question reproducible by including sample data with some that meets and some that does not meet your filtering criteria. See https://stackoverflow.com/q/5963269 , [mcve], and https://stackoverflow.com/tags/r/info – r2evans Jun 01 '23 at 12:30
  • 1
    You need to both (i) use `datframe[[kern_getal_type]]` and (ii) pass `kern_getal_type` as a string: `df_selectie_risico(dataFrAme,"B", "Solvabiliteit: Hoog - Laag")`. Read the help page for `$` – Ricardo Semião e Castro Jun 01 '23 at 12:50

0 Answers0