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.
The function will take a dataframe as input and return it’s manipulated version.
If the variable is an integer, convert it to numeric.
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".
I was unaware of this, thanks for guiding me to the policies!
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.
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.
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.