Function with Rules

Hi! I am new to R and currently working on practice assignments. Whereas I am stuck.

Create a function that will automatically manipulate the variables in any dataframe according to some rules.
:arrow_lower_right: The function will take a dataframe as input and return it’s manipulated version.
:arrow_lower_right: If the variable is an integer, convert it to numeric.
:arrow_lower_right: If the variable is a factor, convert it to a character when the number of levels is bigger than an arbitrary parameter (threshold). Give to that
threshold a default value of 1000.

SO basically, I figured out how to add the first rule, but when I try using else if to add the other rule, I am not able to do it. Also I am not sure how to do the part "when the number of levels is bigger than an arbitrary parameter (threshold). Give value of 1000 to threshold".

Hope my question is clear!

Eager to learn and participate in this community! :slight_smile:

Hi, welcome!

Please have a look to our homework policy, homework inspired questions are welcome but they should not include verbatim instructions from your course.

Also, it would be easier to help you if you make your question in the form of a REPRoducible EXample (reprex) showing what you have made so far.

I was unaware of this, thanks for guiding me to the policies! :slight_smile:

It is not homework, but rather assignments I got from a source to practice R on my own. Under is the code I use for the first part, followed by the output.

function1<-function(df){
  for(variable in colnames(df)){
    if(class(df[,variable])=='integer'){
      df[, variable]<-as.numeric(df[, variable])
    }
  }
  
  return(df)
  
}
df_conditions<-function1(df_conditions)

head(df_conditions)
#        START       STOP                              PATIENT
# 1 2016-04-14 2016-04-28 d171d808-1f31-4ad3-aba5-e03a2fa921c7
# 2 1995-08-18            3603cd65-53a3-424b-bc82-76204326510d
# 3 2010-02-12 2010-02-19 3603cd65-53a3-424b-bc82-76204326510d
# 4 1998-10-19 2009-10-26 bd512b17-9e68-4b0e-8c5b-980007ecdee1
# 5 2009-12-14            bd512b17-9e68-4b0e-8c5b-980007ecdee1
# 6 2009-12-14            bd512b17-9e68-4b0e-8c5b-980007ecdee1
#                              ENCOUNTER      CODE                      DESCRIPTION
# 1 0b4374e8-838d-4289-b491-f44d51360790 444814009       Viral sinusitis (disorder)
# 2 61f6867a-9e58-48e6-87d0-6d9495a81a79  59621000                     Hypertension
# 3 ae97dfe0-c619-44c1-a687-f215d7fac984  10509002      Acute bronchitis (disorder)
# 4 e0213a36-1b17-45c9-a233-31e1042e7b28 192127007 Child attention deficit disorder
# 5 31bd0967-a973-4eaf-bf40-5ca6eb341eb7  15777000                      Prediabetes
# 6 31bd0967-a973-4eaf-bf40-5ca6eb341eb7 271737000                Anemia (disorder)

PS: (Hope I got it right this time)

It is better but it is still hard to help you, since your sample data is not on a copy/paste friendly format and your example is not self-contained, please follow this guide and try to turn this into a proper reproducible example.

Note: Even if you are just practicing R for your own, it is better not to include verbatim instructions, since content might be protected and reproduction restricted.

Use nlevels() to count the levels in a factor.

Can you also share what you've tried for adding an else to the checks? It's hard to help fix a problem with code we haven't seen.

This is just a "best practices" note: replace class(df[,variable])=='integer' with is.integer(df[, variable]). Some objects can have multiple classes, which would mess up your code. There's a similar function for factors.


P.S.: I formatted your code for the forum to make it easier to read. Check out FAQ: How to format your code to see how it's done.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.